Thread: Scanf bug??

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Question Scanf bug??

    The following code compiles without a problem..

    Code:
    #include <stdio.h>
    
    main()
    {
      char s[20],c;
    
      printf("Enter string : ");
      scanf("%s",s);
    
      printf("Enter char : ");
      scanf("%c",&c);
    
      printf("The string is %s and The char is %c",s,c);
    
    }
    but while running it.. after the string is entered, the scanf ( for the char), doesn't wait for it to be entered and immediately proceeds to print the final statement (Tested with GCC and Turbo C++ 3.0). Why is this happening ?

    After a little bit of tweaking...I found out that when the order of the two scanf statements is changed like...

    Code:
    #include <stdio.h>
    
    main()
    {
      char s[20],c;
    
      printf("Enter char : ");
      scanf("%c",&c);
    
      printf("Enter string : ");
      scanf("%s",s);
    
      printf("The string is %s and The char is %c",s,c);
    
    }
    it works correctly as expected.

    My Problem is.. I do not want to change the order (the string should be scanned before the char). What should I do ?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I saw the title and laughed. The \n is still in the buffer, that is why character worked first.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Thumbs up Thanks

    Thanks for the replies..
    I guessed that it should be due to the buffering..(now I know it for sure!)

    Code:
    /* If you are sure that unwanted data is in the input stream,
     you can use some of the following code snippets to remove 
    them. However, if you call these when there is no data in the
     input stream, the program will wait until there is, which gives
     you undesirable results. */
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    I also found out that calling getchar() immediately after the scanf("%c",&c); solves the problem...

    my code now looks like
    Code:
    #include <stdio.h>
    
    main()
    {
      char s[20],c;
    
      printf("Enter string : ");
      scanf("%s",s);
    
      printf("Enter char : ");
      scanf("%c",&c);
      c = getchar();
    
      printf("The string is %s and The char is %c",s,c);
    
    }
    linuxdude: I saw the title and laughed.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Glad you are learning just one thing though. The reason that getchar() is working because c is being used twice. in the scanf("%c",&c);
    gets the \n left in the buffer, and then the c=getchar(); ask the user for the input just making sure you understand that

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Scanf bug??
    scanf is a bug waiting to happen.
    My best code is written with the delete key.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    And what does that make gets( )?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And what does that make gets( )?
    An infestation.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird bug in the scanf command
    By transgalactic2 in forum C Programming
    Replies: 13
    Last Post: 10-24-2008, 03:26 PM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM