954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is EOF in stdin?

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

Majoni
Newbie Poster
5 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

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

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Thanks,
Any idea what is the value of EOF in Linux

Majoni
Newbie Poster
5 posts since May 2007
Reputation Points: 10
Solved Threads: 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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Thanks,
I just checked it out it is Control+d in Linux

Majoni
Newbie Poster
5 posts since May 2007
Reputation Points: 10
Solved Threads: 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 */
}
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 

> 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

gourav1
Posting Whiz in Training
203 posts since Oct 2011
Reputation Points: -2
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You