bluey said:
The actual answer is quite simple. you need to learn - as do many -
about the registers on the cpu itself. given that 0 in acsii is a
value (albiet a Null value) the only way a processor can see past this
is by checking the carry bit flag. by passing a -1 to the register it
can see a processor condition of carry (or not in this case0.
Knowing about CPU registers is not necessary at all, or even
particularly helpful in this case.
fgetc() returns an int value which is either:
the next character read from the input stream (interpreted as an
unsigned char and converted to int;
or:
a distinct value, namely the value of the macro EOF, to indicate
end-of-file or an error condition.
The standard requires EOF to have a negative value. (It doesn't
require it to be -1; I've never heard of an implementation where it's
defined as anything other than -1, but I still wouldn't assume
anything beyond what the standard guarantees).
Since a valid unsigned char value can be any non-negative value in the
range 0 to UCHAR_MAX, it makes sense to use a negative value if you
need something distinct from all possible unsigned char values.
Note the cpomlete lack in this explanation of any mention of CPU
registers, condition codes, or carry bits. It's all about values, not
representations. And since most code checks whether the result is
equal (or unequal) to EOF, the fact that it's negative doesn't
typically even matter. The standard *could* have allowed EOF to be
defined as (UCHAR_MAX + 1), for example.