2

I'm a beginner programmer (forgive this very basic question), and I am learning C through the Kernighan and Ritchie book "The C programming language".

I copied this program from the book, and it compiles fine, but when an input is given, the program does nothing.

#include <stdio.h>

int main() {
   long nc;

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

The output is supposed to be the number of characters in the input, but nothing is happening

  • Well explained here [getchar() != EOF](https://stackoverflow.com/questions/10720821/im-trying-to-understand-getchar-eof) – Achal Jul 21 '19 at 10:35
  • Related: [End of File (EOF) in C](https://stackoverflow.com/q/4358728/3049655) – Spikatrix Jul 21 '19 at 10:40

2 Answers2

2

It will print the number of inputted characters when it has encountered the EOF (=end of file) condition.

If you're providing input through a terminal, there's no natural end of file so you need to signal it with a special keyboard shortcut, which is typically either Ctrl+Z on Windows and Ctrl+D on a Unix system (Linux, MacOs, ...). (Windows also appears to require that you type an Enter both before and after the Ctrl+Z. The new-line character before the Ctrl+Z counts as another character, which effectively means that Windows, unlike Unixes, doesn't appear to allow you to have text-files that don't end with a new-line, at least with mingw gcc without cygwin.)

If you provide the input file through redirection as in ./a.out < some_file, then you don't have to worry about that because filesystem files have natural ends.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • so from what I understand, I have to type ctrl+z straight after the input. Unfortunaetely that still doesn't actually do anything when I press the enter key. – SamiGhidini Jul 21 '19 at 17:11
  • @SamiGhidini Ctrl+Z and Enter after that works for me on Windows. I usually use Linux, though. There it's just Ctrl+D. – Petr Skocik Jul 21 '19 at 17:19
  • 1
    so apparently it works after when I press Enter, followed by Ctrl+Z and Enter again. Obviously the issue with that is that it outputs a number that is too large by one character. Do you know why that is? – SamiGhidini Jul 21 '19 at 17:32
  • Oops. I had missed that part. Yes, a newline before the Ctrl+Z appears to be required on Windows. The new-line character does count as another character, hence the increased count, and unfortunately, the C standard does allow Windows to require it because as per the C standard "Whether the last line requires a terminating new-line character is implementation-defined." (http://port70.net/~nsz/c/c11/n1570.html#7.21.2p2). I'd recommend learning C on Linux or at least Cygwin. C is somewhat more at home at Unixes, & there Ctrl+D simply works and you don't have to mess with Enter before and after. – Petr Skocik Jul 21 '19 at 17:45
1

You are supposed to end the stream with an EOF indicator, which is CTRL+Z on Windows and CTRL+D on Linux based Operating systems. When getchar() reads EOF, it exists the while loop and the number of characters is output to stdout.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93