Re: EOF and getchar/fgetc

Discussion in 'C Programming' started by Martin Dickopp, Feb 14, 2004.

  1. This question seems to be off-topic in comp.std.c, since it is a
    question about the C /language/. Answer cross-posted to comp.lang.c,
    Followup-To set.

    (Mantorok Redgormor) writes:

    > unsigned char will always be a byte and getchar is equivalent to getc
    > and getc is equivalent to fgetc.

    `getchar()' is equivalent to `getc(stdin)'. The difference between
    `getc' and `fgetc' is that the former, unlike the latter, may be
    implemented as a macro which evaluates its argument (i.e. the stream to
    read from) more than once. This only makes a difference if the argument
    is an expression with side-effects.

    > fgetc gets an unsigned char that is converted to an int.

    It reads a character represented as an `unsigned char', and converts it
    to `int'.

    > so we can say fgetc is a function that reads in a byte at a time.

    Yes, `fgetc' reads at most one byte, i.e. it either reads exactly one
    byte and returns that (converted to `int'), or it reads zero bytes in
    the case of error or end-of-file condition. In the latter case, it
    returns `EOF'.

    > now, EOF expands to a negative integer of type int and fgetc returns
    > EOF on error.

    ....or on end-of-file condition.

    > so if fgetc is reading in characters of type unsigned char then
    > converting them to int, this would mean EOF would have to be in the
    > range representable by unsigned char.

    No, `fgetc' returns `EOF' if it has read /no/ character. `EOF' is not
    a character which can be found in a stream, it is used to represent a
    /condition/ (error or end-of-file).

    Also note that `EOF' is guaranteed to be negative, so it cannot be
    represented by an unsigned type.

    (Incidentally, on systems where `sizeof(unsigned char) == sizeof(int)',
    there could be a character that has the same value as `EOF' when
    converted to `int'. In practise, you can often ignore that possibility.)

    > Which means that the value of EOF may never be larger than a byte?

    That question makes no sense: You cannot compare a /value/ to a
    /storage unit/. But, as I said, the value of `EOF' is negative and
    can therefore not be represented by `unsigned char'.

    Martin Dickopp, Feb 14, 2004
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?q?Jos=E9_de_Paula?=

    fgetc() past EOF

    =?iso-8859-1?q?Jos=E9_de_Paula?=, Jan 17, 2004, in forum: C Programming
  2. Luke Wu

    The need for int to capture getchar()'s EOF

    Luke Wu, Jan 23, 2005, in forum: C Programming
    Lawrence Kirby
    Jan 26, 2005
  3. broeisi

    getchar function and EOF problem..

    broeisi, Mar 10, 2006, in forum: C Programming
    Barry Schwarz
    Mar 12, 2006
  4. neras
    Mar 17, 2006
  5. arnuld

    getchar() and EOF confusion

    arnuld, Oct 15, 2008, in forum: C Programming
    David Thompson
    Oct 27, 2008

Share This Page