[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ctrl+d (EOF) and the standard input.



I got an exercise where I have to use ctrl+d (EOF sign, as much as I
understand) with the standard input that is the channel to input data to
a C program.

1) Can you explain me what supose to happen ? I mean, when I use ctrl+d
on the standard input that is passing commands to the shell, I am
exiting the shell. What supoose to happen when I type ctrl+d when the
standard input is passing input to a program ? 

2)I tried to run the following program:

#include <stdio.h>

int main(void)
{
  int input_char, num_of_EOFs = 0;

  do
    {
      if ((input_char = getchar()) == EOF)
	printf("%d EOF signs were detected so far\n", ++num_of_EOFs);
    } while (input_char != 'q');
  exit(0);
}

The results were that if I type ctrl+d after a return key, it
immediately detects the EOF sign. But otherwise I had to press ctrl+d
twice to make the program understands that I want it to detect an EOF.
Why is that ?


Follow-Ups: