Input Buffer problem.

This is a discussion on Input Buffer problem. within the C Programming forums, part of the General Programming Boards category; Okay, for example I have this code, Code: char str[20]; char chr; int num; /* Case 1 */ printf("\nNum: "); ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    Input Buffer problem.

    Okay, for example I have this code,

    Code:
            char str[20];
            char chr;
            int num;        
             
            /* Case 1 */
            printf("\nNum: ");
            scanf("%d", &num);
            printf("\nStr: ");
            scanf("%s", str);
            
            printf("\nLetter: %d", str[0]);
            
            /* Case 2 */
            printf("\nNum: ");
            scanf("%d", &num);
            printf("\nChr: ");
            scanf("%c", &chr);
    
            printf("\nLetter: %d", chr);
    And when I run the code,
    On case 1: the str[0] has value of the first character of whatever you inputted.

    On case 2: the chr has value of LineFeed (ASCII 10).

    Now for my question,
    Why on case 1 the str does not take the LineFeed character ?
    Whereas on case 2, the chr takes the LineFeed character ?

    Aren't both string and character supposed to take everything inputted into them?

    I hope you get my meaning..

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    %s reads the next non-space sequence of chars, skipping any leading spaces, tabs and newlines

    %c reads the next char

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    Hmm, I see, I didn't know that.

    But anyway, thanks for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clearing Buffer Problem (beginners Q)
    By Iconate in forum C Programming
    Replies: 5
    Last Post: 03-18-2008, 01:57 PM
  2. Buffer problem
    By Narcose in forum Game Programming
    Replies: 3
    Last Post: 03-24-2006, 08:29 AM
  3. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 12:50 AM
  4. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 02:50 PM
  5. Clearing the Input buffer
    By Brain Cell in forum C Programming
    Replies: 5
    Last Post: 03-21-2004, 11:08 AM

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