variation of while with scanf

-1

Good evening can someone tell me what this code snippet means, since net searching I did not find follows:

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

Thank you for your attention

    
asked by anonymous 07.11.2018 / 02:46

3 answers

0

The while will run as long as its condition result is true (or 1). Therefore, while scanf("%d", &variavelA) && variavelA results in 1.

In this case the scanf function 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 if the scanf returns 0 or the read value for variavelA is 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 (execute 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; 
}
    
07.11.2018 / 03:20
6
scanf("%d", &variavelA)

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

& is a command that determines that the next block must be repeated as long as 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 parent to not be confused 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 to no longer be executed.

The condition is made up of two subexpressions, the first is i while already explained and you already know if it gives 0 or a larger number. The second subexpression checks the value entered, so to be true you can not enter the value 0. As there is the operator scanf() both expressions must be different from 0 so that the final result is different from 0, and just so it continues to execute. The second subexpression will only be executed if the first subexpression is true, for a concept called short circuit .

Probably wanted to do the opposite, get out of the loop when this is true, that is, it keeps repeating while the data entered is not valid. So:

#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);
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
07.11.2018 / 03:02
-2

It is a somewhat redundant code, meaning you are starting a loop while the program receives input from the user, ie it will be an infinite loop unless you put a handle on it. Repetition of variavelA really is redundant, I suggest you take or drop a treatment for the EOF.

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

    
07.11.2018 / 03:02