Variation of while with scanf

Asked

Viewed 516 times

-1

What does this piece of code mean?

while(scanf("%d", &variavelA) && variavelA )

3 answers

6

scanf("%d", &variavelA)

Manando is reading a data in the console that should be compatible with a decimal number. The value will be placed in the variable variavelA through his reference (&). It will return the number of data read correctly, so it is expected to return the value 1, although it may eventually come higher than this. If reading fails the return will be 0.

The while is a command that determines that the following block should be repeated while the condition within the parentheses is true. note that the most readable is to have a space between the command and the opening of the parenthesis so as not to confuse it with a function. It will be true whenever the total expression in there is different from 0. 0 is reserved for false and carrying a final result 0 will cause the next block of commands not to be executed anymore.

The condition is composed of two subexpressions, the first is i scanf() already explained and already knows if it gives 0 or a larger number. The second subexpression checks the entered value, so to be true the value 0 cannot be entered. As there is the operator && both expressions need to be different from 0 for the final result to be different from 0, and only so continues to run. The second subexpression will only be executed if the first out true, by a concept called short-circuit.

Probably wanted to do the opposite, exit the loop when this is true, IE, it keeps repeating while the entered data is not valid. Thus:

#include <stdio.h>

int main() {
    int variavelA = 0;
    printf("Digite um número maior que zero: ");
    while (!scanf("%d", &variavelA) || !variavelA) {
        printf("Digite um número maior que zero: ");
        getchar();
    }
    printf("%d", variavelA);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

-1


The while will perform while the result of its condition is true (or 1). So, while scanf("%d", &variavelA) && variavelA result in 1.

In that case the function scanf will return 1 when an integer is read (%d). The result of this function will make a logical AND (&&) with variavelA. The result of this logical AND will only be 0 in case the scanf return 0 or read value to variavelA be 0.

See Truth Table for Logical AND (&&):

0 && 0 == 0
0 && 1 == 0
1 && 0 == 0
1 && 1 == 1

So the result will only be 1 (run while) if both entries are 1 (read an integer and it is not 0, in this example).

If you want to test:

#include <stdio.h>

int main() {
    int variavelA = 0;
    while(int i = scanf("%d", &variavelA) && variavelA){
        printf("%d", variavelA);
    }
    return 0; 
}
  • You used std::cout in your example but the question is about C and not C++.

  • Correction made. Thank you so much for editing the "table" as well.

  • 1

    You’re welcome! I was going to make a table just like you did, only using this page someone passed by: ASCII Table Generator, which is very useful sometimes, but in the end I thought it would look better.

-2

It is a kind of redundant code, meaning that you are starting a loop while the program receives input from the user, that is, it will be an infinite loop unless you put a treatment for it. The repetition of variavelA really is redundant, I suggest take or put a treatment for EOF.

When you do not use any value in the scanf, it returns EOF (usually -1), and if you have input, returns 1. The question here is that you will be stuck in the loop unless q use a break during the while to break it.

  • There’s nothing redundant or loop infinite there. There is a probable logic error.

  • There is no mistake, he asked to explain the code, in my opinion is redundant the part I mentioned, and generates a loop like this if n there is a break in while. I saw that your answer was much more complete, so at least mine serves as a complement.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.