Thread: Issues in using char or int for EOF

  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
    4,633
    This link may be of some help.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    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.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    294
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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, 11:06 AM
  5. char* - char[] - sprintf-issues
    By Ryam BaCo in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2005, 02:19 PM

Tags for this Thread