Question

I have started learning C and I have also started using Ubuntu. I am writing the codes in vim through the terminal. I have been studying from Kernighan and Ritchie. Here is the code -

#include <stdio.h>

int main()
{
 int c;
 while ( (c = getchar()) != EOF)
 putchar(c);
 return 0;
 }

Now, my question is :-

  1. The program stops if i press ctrl+z , but it also terminates if i press ctrl+D , when i read about it online it says ctrl+z is the EOF in windows and ctrl+d in linux. Does it mean that both of them are EOF in linux? If so , what are the other EOF ?
  2. Is it okay to have such complex questions in the first chapter of this book ? or am I supposed to just read through it and these kind of thoughts will get cleared as I read on ?
Was it helpful?

Solution

Two different things are happening.

Typing Control-Z in Windows or Control-D in Linux triggers an end-of-file condition and causes getchar() to return the value EOF.

Typing Control-Z in Linux does something different: it suspends the execution of your program. After typing Control-Z, if you type the jobs command, it will show your suspended program. Typing fg will cause it to resume and continue accepting input -- until you signal an end-of-file condition by typing Control-D. That would be easier to see if your program generated some output.

The behavior of Control-Z isn't connected to the way your program is written; Control-Z will suspend (nearly) any running program, whether it's written in C or not, and whether it's waiting for input or not. That behavior is specific to Linux and other Unix-like operating systems.

You'll probably never run out of doubts and questions; I certainly haven't.

OTHER TIPS

  1. CTRL+Z in Windows and CTRL+D in Linux sends EOF value to stdin, EOF itself is implementation defined. Only CTRL+D in linux works. You can print it like printf("%d", EOF); to see its numerical value.

  2. Yes, doubts and thoughts always useful =)

EOF in C is a condition("End of File), not a special character telling you that you have reached the end - there is no more input. This condition is indicated to you by various functions such as getchar() returning the special value EOF. EOF is just a value (often a macro that is #defined to be -1), that otherwise cannot occur in the data you read by these functions.

If you read from a file, or a pipe, you know you've reached the end of that file when getchar() returns EOF. getchar() reads from stdin. You can have a pipe connected to stdin instead of your console by running your program as echo hello | ./theprogram

If you read input from a terminal/console, things are more complex. On some system there are huge layers involved in bringing input from your keyboard into your program.

But the short version is that on some systems, when you hit the CTRL+D (or CTRL+Z on others) key combination, it's able to notify your program that the end of input is reached, which makes getchar() return EOF.

As for CTRL+z on linux, this would suspend(halt, but not terminate) the current foreground process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top