Keith Thompson said:
getch() returns a result of type int. c is of type int. s[0] is of
type char. So this assigns a value of type int (which can be negative
if and only if it's equal to EOF)
Where is this guaranteed? Characters in the basic execution set are
guaranteed to be non-negative when stored in chars (6.2.5), which implies
that they must be non-negative as ints, but other characters (such as
'@') have no such requirement as far as I can see.
Here's the code being discussed:
| while ((s[0] = c = getch()) == '@')
|
| (assume getch() is like getchar(), and that s is an array of char).
The value returned by getchar() (and therefore, by assumption, by
getch()) is either EOF (which is negative), or the next input
character *obtained as an unsigned char* and converted to an int. So
the value returned by getch() (and assigned to c) is negative if and
only if it's equal to EOF; even if '@' < 0, getch() will return a
positive value when '@' is the next character in the input stream.
Also, if char is unsigned, does anything stop EOF, when converted to
a char, matching the value of *any* character, even ' ' (which was
used in the original example)?
As I said in the previous article (and you didn't quote:
| It's possible that EOF, after being converted from int to char,
| happens to match the value of '@' (though not for any real-world
| character set I'm aware of).
More realistically, consider an implementation with 8-bit signed char
using an ISO 8859-1 (Latin 1) encoding, with EOF == -1. Character 255
is a lower case 'y' with an umlaut (two dots). If you read that
character using getchar(), the result is 255; if you then assign it to
a (signed) char, the assigned value is -1, which is EOF.