Like Tree1Likes
  • 1 Post By grumpy

Issues in using char or int for EOF

This is a discussion on Issues in using char or int for EOF within the C Programming forums, part of the General Programming Boards category; I was just reading the marvellous piece on the subject "The C Programming Language" By "Kernighan and Ritchie",it suggested one ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Issues in using char or int for EOF

    I was just reading the marvellous piece on the subject "The C Programming Language" By "Kernighan and Ritchie",it suggested one thing:
    While reading a file character by character,using getchar( ),we must use a large data type(like int) to check for EOF,not char.But here my program(taking char) works fine:

    char c;
    while((c=getchar( ))!=EOF)
    putchar(c);

    Can someone please help in clearing the confusion?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    This link may be of some help.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,437
    Rachit, you either got lucky (in a bad way) or you haven't actually tested your code well enough (with input that causes getchar() to return EOF). EOF is usually a value that a char type cannot represent, so your while loop will never end.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    333
    EOF is -1; When youa ssign it to a char, you get 0xFF and the leading bits are trucated.
    Most text files won't contain character 255 (&ymul; , a smal Latin y with a twiddly bit on top). So the code is fine, until you happen to open such a file.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,688
    Quote Originally Posted by Malcolm McLean View Post
    EOF is -1; When you assign it to a char, you get 0xFF and the leading bits are trucated.
    Most text files won't contain character 255 (&ymul; , a smal Latin y with a twiddly bit on top). So the code is fine, until you happen to open such a file.
    The above assumes char is signed; if unsigned the issue differs in that the EOF can not be detected as posted in prior post.

    Also, EOF is normally -1 NOT always -1.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    The code posted will compile and run, but if you really are reading in a file, it won't finish running. As you already know, the char data type is not large enough in most cases to hold the EOF value defined.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by jwroblewski44
    The code posted will compile and run, but if you really are reading in a file, it won't finish running. As you already know, the char data type is not large enough in most cases to hold the EOF value defined.
    Not really. If char is unsigned, then the value of EOF would be converted to be within the range of char, so the "won't finish running" thing wouldn't happen, but the code would be incorrect. If char is signed, then it depends on what is the value of EOF, but as was mentioned earlier, EOF is often defined as -1, which would fit into a signed char, though the code would still be incorrect.
    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

  8. #8
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    If char is unsigned, then the value of EOF would be converted to be within the range of char[...]
    On my system, if you assign EOF to an unsigned char, it becomes 255. 255 seems to a "non-breaking space" in the extended ascii. Disclaimer: I may be wrong....

    I guess what I'm trying to say is I see no reason not to use the type integer to deal with file I/O and EOF.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Oh, you are right: the promotion of char to int would yield the converted value, which would then be compared to the original value of EOF, hence there would be no value of c that would compare equal to EOF, if char is unsigned.
    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

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,437
    Quote Originally Posted by jwroblewski44 View Post
    On my system, if you assign EOF to an unsigned char, it becomes 255.
    That is how unsigned types work - modulo arithmetic. Similarly, subtract 1 from zero, and the result is the maximum value that the type can represent (255 in the case of a typical 8-bit unsigned char).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. Signed/Unsigned char issues
    By NewbGuy in forum C Programming
    Replies: 14
    Last Post: 07-11-2009, 02:27 PM
  3. Issues with using CHAR with IF/ELSE conditions.
    By TheBlackVeil in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2007, 10:58 AM
  4. new char issues
    By axr0284 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2006, 10:06 AM
  5. char* - char[] - sprintf-issues
    By Ryam BaCo in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2005, 01:19 PM

Tags for this Thread


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