To understand how this works, imagine what was the getchar mindset writing guy. You need to read the file. Start by creating a procedure - for example:
unsigned char get_me_a_byte(file)...
now you want to read all bytes from the file:
unsigned char c; while( c = get_me_a_byte(file) )
The problem is that it will stop when z zero is encountered, but you want to stop when everything is red. Now you are getting smarter - you know that files can be thought of as a sequence of bytes. What if your get_me_a_byte can return a 16 or 32 bit type? Then you can use some value that the byte cannot hold as the end of the file token.
lotto
Since your solution may have:
int get_me_a byte_U(file) ... // returning bytes as 0..255 int get_me_a byte_S(file) ... // returning bytes as -128..127
Now you can do:
int c; while( (c = get_me_a_byte_U(file) != UUU ) ....
where UUU can be anything: from 256 to MAXINT on your platform
Similarly:
int c; while( (c = get_me_a_byte_S(file) != SSS ) ....
where SSS can be anything from MININT ..- 129 and 128..MAXINT
Now, if you chose the first method, the question arises: what should UUU (your EOF) mean?
(- 1) is good for EOF because no matter what bit width of a variable you can assign to it, it will remain (-1). "-1 left", I mean that it will always be the whole template.
char c = -1;
This should now be obvious.
Artur source share