Like Tree16Likes

EOF

This is a discussion on EOF within the C Programming forums, part of the General Programming Boards category; Is ctrl Z called the EOF. If yes, then why doesn't the following code print out -1 when we hit ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    EOF

    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);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    inu11byte likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    EOF ... which is an internal flag,
    what does that mean?? Isn't EOF supposed to be a macro with an integer value -1.

    Quote Originally Posted by CommonTater View Post
    Because you're pressing CTRL-Z ... which is ascii value 26...
    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);
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by juice
    Isn't EOF supposed to be a macro with an integer value -1.
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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()...

    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;
    }
    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.

    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...
    juice and chibi.coder like this.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by CommonTater View Post
    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...
    where are these help files, couldn't find them...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    where are these help files, couldn't find them...
    Join the forums (You have to be a member to download)... search for Win98 SDK

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    The concept EOF can be seen only in file streams (the opened files). EOF is a macro with -1 value.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by chibi.coder View Post
    The concept EOF can be seen only in file streams (the opened files). EOF is a macro with -1 value.
    What part of the preceeding 9 messages did you not understand?
    The actual EOF marker used in early MS-DOS files was ascii code 26... CTRL-Z.
    The EOF macro is a marker returned by C-Library functions when it finds CTRL-Z.
    Last edited by CommonTater; 12-19-2011 at 01:56 PM.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    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.
    Not "any function" ... That's a dangerous assumption. ALWAYS check the syntax in the Library Documentation (help file) to be sure.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,200
    Quote Originally Posted by juice View Post
    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.
    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.
    Right 98% of the time, and don't care about the other 3%.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by grumpy
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 6 123456 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21