Is ctrl Z called the EOF. If yes, then why doesn't the following code print out -1 when we hit ^z..
Code:int main() { int i; scanf("%d",&i); printf("%d",i); }
Is ctrl Z called the EOF. If yes, then why doesn't the following code print out -1 when we hit ^z..
Code:int main() { int i; scanf("%d",&i); printf("%d",i); }
Read the FAQ on Definition of EOF and how to use it effectively
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Because you're pressing CTRL-Z ... which is ascii value 26... Windows *treats it like* an end of file marker.
EOF ... which is an internal flag, is something else altogether
and feof(file); is a third thing entirely.
what does that mean?? Isn't EOF supposed to be a macro with an integer value -1.EOF ... which is an internal flag,
How can we verify the ASCII value of CTRL-Z, the following code would print it out as 0....
Code:#include<stdio.h> #include<stdlib.h> int main() { char i; scanf("%c",&i); printf("%d",i); }
In the C standard library, EOF is a macro with a negative integer value. It is a flag in the sense that it is a special value returned by certain functions to indicate end of file.Originally Posted by juice
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You have to understand what happens when you press CTRL-Z with scanf() and similar functions...
Yes your code sample prints 0 ... but you aren't checking the return value of scanf()...
What happens is that when you press CTRL-Z scanf() consumes the SUB character from the input stream, converts nothing, and returns -1 (EOF) to signal that you are done making entries... Hence the common coding of while (scanf(" %c",&i) != EOF) that you will see used all over the place... It's not testing the value of i ... it's looking for the CTRL-Z return from scanf() to break the loop.Code:#include<stdio.h> #include<stdlib.h> int main() { char i = 0; int z; z = scanf("%c",&i); printf("i = %d z = %d", i, z); return 0; }
You're on Pelles C ... so just put your text cursor on the highlighted word scanf in your source code (i.e. click on it) and press F1 ... Voila ... everything you need to know about scanf() ... part way down the page Pelle explains that scanf() is really fscanf(stdin,...) if you click the fscanf() link you will get the rest of the story. That's one of the reasons I recommend Pelles C so highly for a beginner... it puts complete library documentation literally at your finger tip... don't hesitate to use it, everything you need is there.
Plus, if you do a little searching on Pelles Forums you will find there is a set of Windows API help files that can be added to the F1 search giving you help on about 80% of the Windows API as well...
The concept EOF can be seen only in file streams (the opened files). EOF is a macro with -1 value.
Last edited by CommonTater; 12-19-2011 at 02:56 PM.
If I am not wrong, EOF is just a value that any function returns when it has reached the last character of a file or, when it encounters ctrl-z.
so its not a character which becomes part of the file. It is only a value which functions like getc() or scanf() would return when they reach the end of file, or encounter ctrl-z.
Correction. EOF is a value that is returned by I/O functions when they encounter the end of a file or stream.
The end of a file or stream is not universally marked with a CTRL-Z. With unix systems, for example, the end of a file is often marked with ascii 4 (CTRL-D).
With a lot of file systems, end of file is not determined by any particular character. It is the logical reaching the end of file that matters.
Incidentally, there is no requirement that EOF have the value -1. It is an integral value that is typically outside the range of values that can be stored in a char type. If the char type is unsigned, -1 will suffice. If the char type is signed, -1 may not suffice.
There is no such requirement, but in practice -1 will always suffice since any confusion due to char being signed is removed by the conversion to unsigned char before the value is returned as an int.Originally Posted by grumpy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)