Thread: The variable is unexpectedly set zero while running while loop

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    18

    The variable is unexpectedly set zero while running while loop

    Hello, I am a new learner of C.

    I ran into a problem while using while loop.

    The declared and initiated local int variable works well with its specified value while running through the 1st run of a while loop.

    It is set zero while entering the 2nd run and the following unexpectedly.

    However, the variable still exits.

    I can't understand its mechanism please help me.

    Thanks a lot

    The following is the code with problem.

    Code:
    #include <stdio.h>
    
    
    int main(void){
        
        const int x=6;
        char c='y';
         
         while(c=='y'){
                 //const int x=6;
                 printf("x = %d\n",x);
                 printf("Enter:");
                 scanf("%s",&c);   
                 printf("\n");  
                 }         
                  
        system("pause");
        return 0;          
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    scanf("%s",&c);
    c is just a char. It cannot be used to store a string, except maybe an empty string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    because of the fact that you're reading a string into a char location, it's likely that the terminating \0 is wiping out the contents of x. This is a pretty clear case of undefined behavior.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Thanks a lot!

    Another problem raised is while loop breaks in the 2nd run as I use %c in replace of %s.

    I can input the value of char variable using scanf with %c outside loop block without any error.

    I don't realize that?

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Thank you so much!

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Quote Originally Posted by HolmesChang View Post
    Thanks a lot!

    Another problem raised is while loop breaks in the 2nd run as I use %c in replace of %s.

    I can input the value of char variable using scanf with %c outside loop block without any error.

    I don't realize that?

    I think I might figure out why it still output unexpectedly.
    After calling scanf, I input y and pressed enter which is recognized as a character.
    scanf in the first loop read y and leave \n in the stdin, which is read by scanf in the second loop.
    Then loop ended.

    I use fflush(stdin) after scanf to clear stdin, then no character left to be mis-read.

    I am not sure if my explanation is correct although it works.


    Code:
    #include <stdio.h>
    
    
    int main(void){
        
        int x=6;
        char c[6];
        
        c[0] = 'y';
        c[1] = 'a';
        c[2] = 'l';
        c[3] = 'l';
        c[4] = 'o';
        c[5] = 0;    
    
    
         while(c[0]=='y'){
                 //const int x=6;
                 printf("x = %d\n",x);
                 printf("Enter:");
                 c[0] = scanf("%c",&c[0]);
                 fflush(stdin);
                 //getchar();   
                 printf("\n");  
                 }
               
         
         
         //scanf("%c",&c[5]);
         printf("%s\n",c);    
                  
        system("pause");
        return 0;          
    }

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    Thank you.

    I read the usage of fflush from a sample code of a textbook.
    It talks about how to escape from reading the unwanted \n after getchar();
    It also use a following getchar() to "absorb" the unwanted \n in the other way.
    I think I can use the second way.

    BTW, I still have another problem.
    Is it supposed to stop and wait for some input and a following enter as I call scanf?
    Then why scanf automatically read \n from input buffer in the 2nd run of the while loop?
    Still looking up the textbook.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by HolmesChang View Post
    Is it supposed to stop and wait for some input and a following enter as I call scanf?
    Then why scanf automatically read \n from input buffer in the 2nd run of the while loop?
    It is not exactly right.

    What you enter on the keyboard will be put into the stdin buffer only after you press enter.

    scanf reads what is in the buffer, and wait only when buffer is empty.

    So if you press 'y' <ENTER> 2 chars are posted in to the buffer 'y' and '\n'

    So if first scanf reads 'y' and nothig has being done with '\n' the input buffer will not be empty when second scanf starts reading.

    So it will not wait - it will try to read '\n'
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I don't quite understand your question but perhaps try:

    Code:
    scanf(" %c",&c); // note the leading space before the %c
    This will consume any characters that isspace() would return true for (\n included)

  11. #11
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Quote Originally Posted by vart View Post
    It is not exactly right.

    What you enter on the keyboard will be put into the stdin buffer only after you press enter.

    scanf reads what is in the buffer, and wait only when buffer is empty.

    So if you press 'y' <ENTER> 2 chars are posted in to the buffer 'y' and '\n'

    So if first scanf reads 'y' and nothig has being done with '\n' the input buffer will not be empty when second scanf starts reading.

    So it will not wait - it will try to read '\n'
    I think it's the point to which I didn't know before.
    Now I know it.
    Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable value changed unexpectedly
    By johnsack in forum C Programming
    Replies: 2
    Last Post: 03-08-2012, 11:27 AM
  2. loop is running twice
    By Nikitaatwork in forum C Programming
    Replies: 4
    Last Post: 11-08-2011, 06:42 PM
  3. Can't get loop to keep running
    By mytrademark in forum C Programming
    Replies: 2
    Last Post: 11-01-2011, 06:08 PM
  4. Replies: 3
    Last Post: 06-04-2011, 10:07 AM
  5. Replies: 2
    Last Post: 02-28-2002, 03:27 PM