Find the answer to your Linux question:
Results 1 to 8 of 8
Hi all , I got a problem with scanf() in the below code #include<stdio.h> int main() { char ch; int d; printf("enter number\n"); scanf("%d",&d); //fflush(stdin); printf("enter character\n"); scanf("%c",&ch); printf("%d\t%c\n",d,ch); return ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! vinall's Avatar
    Join Date
    Jun 2006
    Location
    bangalore,INDIA
    Posts
    42

    A problem with scanf()?


    Hi all ,
    I got a problem with scanf() in the below code
    #include<stdio.h>

    int main()
    {
    char ch;
    int d;
    printf("enter number\n");
    scanf("%d",&d);
    //fflush(stdin);
    printf("enter character\n");
    scanf("%c",&ch);
    printf("%d\t%c\n",d,ch);
    return 0;
    }
    when this code run, it is not stopping at second scanf for input a character why it is happening and how can i solve this. I tried by using fflush(stdin) between them but it failed. can anybody give the solution for this and i Want to know what actually the reason for its behaviour??

    I am using gcc (GCC) 4.0.0 20050519 (Red Hat 4.0.0-

    ~
    ~

  2. #2
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    Strange behavior. Try to flush (also) stdout. Maybe some outputted character will be caught by the second scanf.

    What values are stored in "ch"?

    Try to execute the program in a debugger. Maybe it helps.
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,258
    Hi.

    I don't do much c programming, but the manual I looked at (O'Reilly, Practical C) says that scanf is notoriously bad for handling numbers. They suggested sscanf instead.

    I adjusted your code thus (enclosed in CODE blocks for easy reading):
    Code:
    #include<stdio.h>
    
    int main()
    {
            char line[100];
            char ch;
            int d;
            printf("enter number\n");
            fgets(line,sizeof(line),stdin);
            sscanf(line,"%d",&d);
            //scanf("%d",&d);
            //fflush(stdin);
            printf("enter character\n");
            scanf("%c",&ch);
            printf("%d\t%c\n",d,ch);
            return 0;
    }
    And, after compilation, the execution shows:
    Code:
    % ./a.out
    enter number
    22
    enter character
    x
    22      x
    Best wishes ... cheers, drl

    ( edit 1: typo )
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Just Joined!
    Join Date
    Jun 2006
    Posts
    22
    Quote Originally Posted by vinall
    Hi all ,
    I got a problem with scanf() in the below code
    Allow me to explain.

    If you enter say '44' and 'x' as input to your original program you have in fact entered 5 characters into the input stream: '4','4',<enter>,'x', and <enter>.

    scanf("%d", &d) reads the first two characters and converts them into the integer value 44 but leaves the newline character (<enter>) in the input stream. The next scanf("%c", &ch) reads the next character (the newline) from the input and prints it out. This is because "%c" does not skip whitespace as "%d" does.

    Drl's version works because 'fgets' reads a complete line, including the newline character, from the input.

    You could also get by with only doing a slight modification to your original program by explicitly telling scanf to skip any whitespace before the character that you want to read:

    Code:
    scanf(" %c", &ch);   /* added a space before '%' */

    And yes, writing solid bug-free input handling is always tricky.

  5. #5
    Just Joined! vinall's Avatar
    Join Date
    Jun 2006
    Location
    bangalore,INDIA
    Posts
    42
    Thank U all ,
    Its working I clearly understood whats happening.But why ???flush is not working here in linux ????????.bcoz the same problem can be solved using flush in windows.!!!!!!!! can any body explain???

  6. #6
    Just Joined!
    Join Date
    Jun 2006
    Posts
    22
    Quote Originally Posted by vinall
    Thank U all ,
    Its working I clearly understood whats happening.But why ???flush is not working here in linux ????????.bcoz the same problem can be solved using flush in windows.!!!!!!!! can any body explain???
    As far as I can remember, the behaviour of fflush is undefined for 'stdin'. Even though it might have the desired effect on some platforms it is not the preferred way of eliminating characters in the input stream.

  7. #7
    Just Joined!
    Join Date
    Aug 2006
    Location
    hyderabad
    Posts
    1

    solution of scanf() problem

    hi,
    After scanning integer variable you are pressing either enter or space to take it as int. Next scanf("%c",&ch) will read that space or enter entered by you. Thats why it is not accepting your choice. So leave a blank space after %d in scanf() like scanf("%d ",&d); or use a getchar() function after scanf("%d",&d); statement. That will solve your problem
    ok
    bye
    srikanth:

  8. #8
    Just Joined!
    Join Date
    May 2009
    Posts
    1
    fflush(stdin);
    before scanf also helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •