LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   EOF counter in C problem (https://www.linuxquestions.org/questions/programming-9/eof-counter-in-c-problem-771999/)

Cyhaxor 11-27-2009 04:14 PM

EOF counter in C problem
 
Hello there,

I am reading a book about C "The C programming language" and I am implementing an example code to count the input characters.

Code:

#include <stdio.h>

/* count characters in input; 1st version */
main()
{
    long nc;
    nc = 0;
    while (getchar() != EOF)
      ++nc;

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

The problem is that I don't know how to terminate this program and get the result. I know that the value of EOF = -1, but giving -1 as the input the program takes it as another input and continues to prompt me for the next input.

Any idea how I can terminate this program??

Thanks in advance.

Dan04 11-27-2009 04:23 PM

Ctrl+D

Cyhaxor 11-27-2009 04:27 PM

That worked! Thank you very much. :)


All times are GMT -5. The time now is 04:31 PM.