Home > Back-end >  if statement doesnt work in C despite meeting condition
if statement doesnt work in C despite meeting condition

Time:10-24

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{
    int c;
    while(true)
    {
        c = scanf("%d", &c);
        if(c==12)
        {
            printf("goes in");
            break;
        }
    }
    return 0;
}

I am still learning C and I got stuck here. My code is pretty simple but I have no idea why it's not working. The while loop functions normally however I cannot access the if statement despite meeting the condition.

Thank you!

CodePudding user response:

The scanf function returns the number of patterns matched. So if you type a number and press ENTER, that number will first be stored in c by the function, then it will return the value 1 to indicate that 1 value was read. That return value of 1 is then assigned to c, overwriting whatever number you entered.

Skip the assignment, and c will have the value you entered.

scanf("%d", &c);
if(c==12)
{
    printf("goes in");
    break;
}

Or better yet, assign the return value to a different variable to see if a number was in fact entered.

int rval = scanf("%d", &c);
if(rval == 1 && c==12)
{
    printf("goes in");
    break;
}

CodePudding user response:

scanf is full of pitfalls. (http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html). As a beginner, you would do well to avoid it completely. (As a programmer of any level, it is wise to avoid it!). To do even this simple program robustly, you must do a few things. Consider:

#include <stdio.h>
#include <stdlib.h>


int
main(void)
{
    int c, rv;
    while( (rv = scanf("=", &c)) != EOF ){
        if( rv == 0 ){
            int k;
            /* Consume a line of input */
            while( (k = getchar()) != EOF && k != '\n' ){
                ;
            }
        } else if( c == 12 ){
            printf("goes in\n");
            break;
        }
    }
    return 0;
}

Since c was not initialized, the program's behavior is undefined if you attempt to read its value without first assigning it. scanf may return without assigning a value, and if you do not check the value returned by scanf to ensure that it is 1, you cannot read the value of c. Also, using %d is always subject to undefined behavior, since it is possible to read a string that represents a value that cannot fit in an integer. By using =, we avoid that possibility (the language standard specifies that an integer can hold all integer values between -99 and 999). Finally, you need to consume data if scanf returns 0, since a return of 0 indicates that scanf did not consume any data. (That's not quite what a zero return means, but it is in this case.) If you attempt to call scanf again without consuming data, the program will sit in a spin loop.

Your best bet as a beginner is to simply stop using scanf.

CodePudding user response:

As already was pointed to you are reassigning the variable c that gets its value in the call of scanf

scanf("%d", &c);
           ^^^^

with the return value of the function scanf

c = scanf("%d", &c);

According to the C Standard (7.21.6.4 The scanf function)

3 The scanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

I have written my answer because I'd like to suggest to rewrite the while loop the following way

#include <stdio.h>

int main( void )
{
    int c;
    while ( scanf("%d", &c) == 1 )
    {
        if ( c == 12 )
        {
            printf( "goes in\n" );
            break;
        }
    }

    return 0;
}

Or as the variable c is used only within the body of the while loop then it is better to use for loop in the scope of which the variable c will be declared. You should declare variables in minimum scopes where they are used.

#include <stdio.h>

int main( void )
{
    for ( int c; scanf("%d", &c) == 1; )
    {
        if ( c == 12 )
        {
            printf( "goes in\n" );
            break;
        }
    }

    return 0;
}
  • Related