Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By Elkvis
  • 1 Post By vart

The variable is unexpectedly set zero while running while loop

This is a discussion on The variable is unexpectedly set zero while running while loop within the C Programming forums, part of the General Programming Boards category; Hello, I am a new learner of C. I ran into a problem while using while loop. The declared and ...

  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
    22,256
    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.
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    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.
    Salem likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  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
    2,764
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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'
    HolmesChang likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 07:08 PM
  4. Replies: 3
    Last Post: 06-04-2011, 11:07 AM
  5. Replies: 2
    Last Post: 02-28-2002, 03:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21