1. Not finding help here? Sign up for a free 30min 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!

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>
    main()
    {
        long nc;

        nc=0;
        while (getchar() !=EOF)
            ++nc;
        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

    mgb_phys

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Very simple C program not doing anything (character counting)
Loading...