int c;

c = getchar();
while ((c = getchar()) != EOF)
putchar(c);
c = getchar();

I know EOF means end of file. Basically, I believe this functions is instructed to keep inputing text from the keyboard until EOF becomes the subsequent input. However, I am not sure what EOF is on the keyboard or what triggers it.

Also, I am trying to understand why c has to be an integer and not a character for EOF.

Recommended Answers

All 4 Replies

I am not sure what EOF is on the keyboard or what triggers it.

If you're on Windows, the Ctrl+Z key combination sill signal end-of-file and cause getchar to return the EOF flag rather than extract a character from the stream. On Unix/Linux, the key combination is Ctrl+D.

thanks for the help

I know EOF means end of file. Basically, I believe this functions is instructed to keep inputing text from the keyboard until EOF becomes the subsequent input. However, I am not sure what EOF is on the keyboard or what triggers it.

Also, I am trying to understand why c has to be an integer and not a character for EOF.

EOF is not a key in your keyboard but rather a condition that the OS sets when it reaches it. In MS Windows for example Ctlr-Z does trigger it. In C programming EOF is implemented as a macro that expands to -1 and it is returned by many functions to indicate such condition, or an error.
getchar() is one of those functions, and its prototype is int getchar ( void ); Notice the return.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.