Thread: getchar, putchar & while is confusing me

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    getchar, putchar & while is confusing me

    I am in the process of learning C programming and K & R is the book of choice. While going through book examples on getchar & putchar; I decided to tweak the example a bit (just to have a better understanding). Now I am confused.

    Code:
    #include <stdio.h>
    main()
    {
    
    int c;
    
    c = getchar();
    
    while (c != EOF)
    {
    printf ("I knew youd type: ");
    putchar (c);
    c=getchar();
    }
    }
    Output:
    Code:
    4
    Somehow I know youd type:4Somehow I know youd type:
    My expected/desired output should have been:
    Code:
    4
    Somehow I know youd type: 4
    The question is why the comments within printf is getting printed again. The program should have stopped at getchar() which was within the while statement block? Why isn't that happening? Can anyone please help me in clearing my logic here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    "4\n" is two characters, so you go round the loop twice.

    If you want to ignore the newline at the end of each line of input, you need to add that to the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Quote Originally Posted by Salem View Post
    "4\n" is two characters, so you go round the loop twice.

    If you want to ignore the newline at the end of each line of input, you need to add that to the code.
    Thanks! Ummmm...although a new line is not a consideration for me. All I want is program should pause for new input after executing the first getchar & putchar

    Code:
    <<input from keyboard>>4
    Somehow I know youd type:4 <<wait now for new input>>

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Wishing C wouldn't treat enter-key as a character won't make it so; enter-key is a character and will be read in by getchar() every time you push it. If you want to ignore it, you'll have to do the ignoring in your code.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alter.ego View Post
    Thanks! Ummmm...although a new line is not a consideration for me. All I want is program should pause for new input after executing the first getchar & putchar

    Code:
    <<input from keyboard>>4
    Somehow I know youd type:4 <<wait now for new input>>
    I don't think you understood what Salem was telling you...
    Ok... you're running the program...
    Now you type the number 4 ... that's one character but it's still waiting for something
    So now you have to press Enter ... that's another character.
    So your input buffer now has 4 and \n...
    getchar() takes off and your loop prints your first keystroke...
    now the loop is in control....
    getchar() now finds the \n left behind by the first getchar()
    It decides you didn't enter anything and off it goes... your loop executes again
    Now the buffer is empty and it will sit at getchar() waiting for new input.

    EVERY keyboard entry you make produces this extra character... so you get to deal with it in your code...

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks Commontater, Tabstop & Salem. I got it now. So apparently the program always waited at first getchar() & not the 2nd getchar() as I initially suspected. A big big thanks again guys!

  7. #7
    Registered User Alexander.'s Avatar
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    I believe you can put the getchar() in the while loop constraints (and removing the first one) so that it evaluates the second time that it is a newline before it even runs (also checking for EOF)

    There are various methods to grab all remaining newlines and there is no real standard one function to do this, although you best keep things simple to prevent newlines from messing up your loops.

    I am glad you have spotted this problem and had asked about it! Always good to understand these cases that sometimes are sparsely noted.
    Last edited by Alexander.; 05-14-2011 at 09:24 PM.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Correct. These little things are never captured in book. Either your tutor points them to you or the good folks at these forums.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by alter.ego View Post
    Correct. These little things are never captured in book. Either your tutor points them to you or the good folks at these forums.
    I don't know why it is so hard for people to grasp this concept. You have pressed two keys on your keyboard. You are calling a function that takes one key. Why is it so difficult to understand that 2-1 = 1?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I don't know why it is so hard for people to grasp this concept. You have pressed two keys on your keyboard. You are calling a function that takes one key. Why is it so difficult to understand that 2-1 = 1?


    Quzah.
    Yeah but we all know the Enter key isn't a real key...

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Perhaps we need this topic to be Sticky on the C Programming form. I would imagine, the sort of issue gets asked several times and something which the newbie will have to get use to these things by reading the sticky posts before asking.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() and putchar()
    By kawaikx15 in forum C Programming
    Replies: 5
    Last Post: 04-13-2011, 11:02 AM
  2. getchar putchar
    By chess2009 in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 02:44 AM
  3. getchar and putchar
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 03-11-2009, 10:29 PM
  4. need help please! getchar, putchar, etc.
    By sue in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 08:40 PM