EOF in C

M

MJ

Hi
I m using getchar() fun. I want to give the EOF from the console
and then check it inside the prog wether its a EOF or not
how can I check it ....
what input shall i give
Mayur
 
W

Walter Roberson

I m using getchar() fun. I want to give the EOF from the console
and then check it inside the prog wether its a EOF or not
how can I check it ....
what input shall i give

The mechanism for signaling EOF to a program is OS dependant.

On Unix systems it is usually by pressing control-D, but that
depends upon you not being in raw input mode, and depends upon
what your line discipline has set for eof, and often depends upon
whether you are at the beginning of a line or not (otherwise the
line discipline might treat control-D as a character deletion).

On DOS it would usually be control-Z instead of control-D, with
completely different line discipline rules.


To test for EOF in your program, be sure that you have #include'd
the proper headers, and then just compare the input character to EOF
But be careful -- getchar() returns a signed integer, not a char
and on many implementations, EOF is NOT representable as a char
(but is certain to be representable as a signed integer.)
 
C

CBFalconer

MJ said:
I m using getchar() fun. I want to give the EOF from the console
and then check it inside the prog wether its a EOF or not
how can I check it ....
what input shall i give

#include <stdio.h>

int main(void) {

int ch; /* note int, not char */

while (EOF != (ch = getchar())) {
/* do something with ch */
}
/* EOF detected, do not use stdin any more */
return 0;
}

and how you signal EOF from the keyboard varies with the system.
For windoze and MSDos based systems it is usually CTRL z
(immediately following an <enter>, or before any other data entered
on a line). On unix/linux machines it is usually CTRL d. Read
your manuals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,446
Latest member
Pycoder

Latest Threads

Top