1

When reading characters from standard input what does it mean when you are asked to sop on hitting EOF in the input?

5
Contributors
7
Replies
11
Views
10 Years
Discussion Span
Last Post by gourav1
0

End Of File.
It is operating system depended.
In Windows for example is iqual to the value -1.

0

In Windows if you enter the keys Control + Z you get EOF, in Linux is Control + D I believe.
So, do a little code like

char ch = getchar();

and enter the EOF. Then

printf("%d", ch );

will display the value.

1

> char ch = getchar();
It should be
int ch = getchar();

char holds all possible chars. To hold all possible chars and EOF, you need a bigger data type, thus int.

Nor is the returned value guaranteed to be -1, which is why there is a constant EOF declared for this purpose.

Eg.

int ch;
while ( (ch=getchar()) != EOF ) {
  /* do stuff */
}
Votes + Comments
right!
0

> char ch = getchar();
It should be
int ch = getchar();

char holds all possible chars. To hold all possible chars and EOF, you need a bigger data type, thus int.

Nor is the returned value guaranteed to be -1, which is why there is a constant EOF declared for this purpose.

Eg.

int ch;
while ( (ch=getchar()) != EOF ) {
  /* do stuff */
}

good answer!! this is the solution

Votes + Comments
I'm certainly glad we waited 5 years for you to give your approval to this ancient piece of information...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.