1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Very simple C program not doing anything (character counting)

  1. May 26, 2010 #1
    1. The problem statement, all variables and given/known data
    Code (Text):
    #include <stdio.h>
        long nc;

        while (getchar() !=EOF)
        printf("%ld\n", nc);

    2. Relevant equations

    3. The attempt at a solution

    I'm trying to learn C and am using the book "C Programming Language". It says that the above program counts characters. But, when I run it and enter something, it just skips down to the next line without doing anything. There's a similar code in the next section that counts lines, and it does the same thing. Am I missing something?
  2. jcsd
  3. May 26, 2010 #2


    User Avatar
    Science Advisor
    Homework Helper

    You are probably trying this on a machine without a raw terminal.
    On windows (in a cmd prompt) or any unix shell it will buffer a line of text before sending it to the program.

    Try typing something and then doing ctrl-z (windows) or ctrl-D (unix) to signal end of file and send the input to your program.

    On unix you can set the terminal to raw mode for this example, it's a bit confusing but worked perfectly well with a raw teletype in 1979 when C was written.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook