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