A mistake in the K & R bible?

This is a discussion on A mistake in the K & R bible? within the C Programming forums, part of the General Programming Boards category; Hello, the following excerpt is taken from "The C Programming Language" by K & R: Code: main() { int c; ...

  1. #1
    kkk
    kkk is offline
    Registered User
    Join Date
    May 2011
    Posts
    32

    A mistake in the K & R bible?

    Hello,

    the following excerpt is taken from "The C Programming Language" by K & R:

    Code:
    main()
    {
           int c;
           while ((c = getchar()) != EOF)
               putchar(c);
    }
    
    The while gets a character, assigns it to c, and then tests whether the character was the end- of-file signal. If it was not, the body of the while is executed, printing the character. The while then repeats. When the end of the input is finally reached, the while terminates and so does main.
    The code doesn't work as described, though. The entered characters are printed only after I press Enter or enter the EOF signal. They are not printed one by one as the authors suggest.

    This is very strange programatically and very difficult for me to grasp. Eg. I enter ten characters and press Enter and only then is the body of the loop executed ten times... Strange... Any comments that might make this a bit clearer for me?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    The C lang bible was written for coders on the Unix platform.
    Do you happen to be working on a Unix or Linux platform?

    Edit:
    argh! I misread the post.
    putchar() buffers stdout until it sees a newline '\n'.
    see the manpage of putchar() for more information.
    Last edited by itCbitC; 07-28-2011 at 11:37 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Input is basically always line-buffered, meaning the input doesn't go to the program until you press enter.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Modern OSs like Windows and Linux use buffered I/O... generally not delivering a keyboard stream to your code until you press enter. Then it will forward the entire buffer at once.

    It's not an error in either the OS or K%R... it's just the way these things work.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Triple Play! ... 3 in one minute.... LOL.
    Last edited by CommonTater; 07-28-2011 at 11:34 AM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    stdin is buffered, so what you type at the command prompt isn't transmitted to your program until you hit enter or the EOF key (Ctrl-D in Linux, Ctrl-Z in Windows).

    It's worth noting that stdout is also buffered. The output buffer typically isn't flushed until one of the following happens:
    1. You ask the user for more input
    2. A new line ('\n') is printed
    3. The buffer is manually flushed with fflush(stdout)
    4. The buffer is filled up without encountering a new line, and is flushed automatically to make room for more output

    But just because you can force out the output with fflush(stdout), don't think you can fflush(stdin). It's undefined behavior, probably wont work and might even crash your program.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    This is a list of known errors in K&R
    Errata for The C Programming Language, Second Edition
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1337 bible, Gen 11
    By Paz_Rax in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-20-2005, 10:40 PM
  2. Bible program - search functionality
    By ChadJohnson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-16-2005, 12:33 AM
  3. Need name for Bible Program
    By ChadJohnson in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 04-14-2005, 09:24 PM
  4. The Ebonics Bible
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-13-2003, 10:26 PM
  5. Visual C++ 6 Bible [Chapter 7]
    By Dual-Catfish in forum Windows Programming
    Replies: 6
    Last Post: 03-12-2002, 01:15 PM

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