views:

470

answers:

4

in the example:

#include <stdio.h>

main()
{
    long nc;

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

I don't quite understand it. putchar() would put the character out, but why is it that after EOF it puts all the characters out, and where is it remembering all these characters? Thanks.

+1  A: 

No where, this code only empty the input and write how many caracters where left before the flush.

This is to be sure that the is no caracters remaining in the input file (stdin)

Karaziox
+1  A: 

It's called buffering and it's done by the operating system. Usually it does line buffering where it just saves every character you put to it in memory, and then writes it all to the file when it encounters a line break. This saves on resources because file operations take much more time than other operations. So instead of doing output with every single character, it waits for a bunch of characters to collect in the buffer and writes them out all in one go.

It's just a clever maneuver done by the OS that you, the programmer, don't need to worry about. Just throw your characters at it one by one and let the OS handle the rest in its own way.

yjerem
+1  A: 

Let's review your code:

#include <stdio.h>

So far so good...

main()
{
  1. The main() function always returns an int. Always.
  2. Declaring functions without return type is poor form, as it defaults to int, but people don't remember this easy when reading code.

Moving on ...

long nc;

nc = 0;

Good form. The variable is initialized before it is used.

while (getchar() != EOF)
    ++nc;

This is a little complex. But one step at a time.

  1. The getchar function returns an int from the standard input.
  2. That value is then compared to EOF.
  3. If the int value from standard input is not EOF, the variable nc is incremented.

So to exit the while loop, an EOF must be generated from standard input.

Note: None of the values read from the standard input are saved. Each value is discarded after the expression is evaluated.

Style: I suggest using '{' and '}' after each while statement. This is a safe habit to get into.

printf("%ld\n", nc);

The above statement is printing (displaying) the value in nc. The value in nc represents the number of characters read from standard input. This statement does not display any characters from standard input.

}

Lastly, the main program must return a value to the operating system. Two portable values are EXIT_SUCCESS and EXIT_FAILURE, which are defined in stdlib.h. Specifying one of these values would also be a good habit.

BTW, there is no putchar in the example you posted (which I copied into this answer).

Thomas Matthews
A: 

[This isn't an answer, but you can't put code in the comments]

I think you meant something like this:

#include <stdio.h>

main()
{
    long nc;
    nc = 0;
    char c;
    while ((c = getchar()) != EOF)
    {
       putchar(c); /* prints one char */
        ++nc;
    }
    printf("%ld\n", nc); /* prints the number of characters read */
}
egrunin