F
Flash Gordon
Barry Schwarz wrote, On 06/08/08 03:08:
You have missed the point (although the bit about CHAR_MIN=EOF is
irrelevant I believe). To take a concrete example imagine an
implementation where CHAR_BIT is 16 and sizeof(int)==1 that uses 2s
complement. On this implementation
UCHAR_MAX==65535
INT_MAX=32767
EOF==-1
In the code you to
int ret = putc(EOF,stdout);
-1 is passed to putc.
putc converts it to an unsigned char giving a value of 65535
putc outputs 65535 to stdout (or the file of your choice)
put *attempts* to return 65535, but as it does not fit in an int
(greater than INT_MAX) it cannot. So putc returns the bit-pattern of all
16 bits set (as that is how 65535 is represented in an unsigned char on
this implementation).
All 16 bits set, when interpreted as a 16 bit 2s complement number
(which is what int is on this implementation) is -1.
Therefore, on this implementation, putc returns -1, which is the value
of EOF on this implementation, when -1 (the value of EOF) is passed in
and putc succeeds.
There have previously been long discussions here about whether such a
hosted implementation is conforming. There are definitely a lot of
freestanding implementations with sizeof(int)==1, but they are not
required to provide putc.
CHAR_MIN is irrelevant since the only char at issue is unsigned char.
UCHAR_MIN, if it existed, would have to be 0. putc only puts unsigned
char. In the absence of an error, it will return the value of the
char that was put. That value is unsigned and therefore non-negative.
Since EOF must be negative, the only time EOF can be returned is when
an error is detected. In the case of an error, the value of the
original int to be put is irrelevant.
You have missed the point (although the bit about CHAR_MIN=EOF is
irrelevant I believe). To take a concrete example imagine an
implementation where CHAR_BIT is 16 and sizeof(int)==1 that uses 2s
complement. On this implementation
UCHAR_MAX==65535
INT_MAX=32767
EOF==-1
In the code you to
int ret = putc(EOF,stdout);
-1 is passed to putc.
putc converts it to an unsigned char giving a value of 65535
putc outputs 65535 to stdout (or the file of your choice)
put *attempts* to return 65535, but as it does not fit in an int
(greater than INT_MAX) it cannot. So putc returns the bit-pattern of all
16 bits set (as that is how 65535 is represented in an unsigned char on
this implementation).
All 16 bits set, when interpreted as a 16 bit 2s complement number
(which is what int is on this implementation) is -1.
Therefore, on this implementation, putc returns -1, which is the value
of EOF on this implementation, when -1 (the value of EOF) is passed in
and putc succeeds.
There have previously been long discussions here about whether such a
hosted implementation is conforming. There are definitely a lot of
freestanding implementations with sizeof(int)==1, but they are not
required to provide putc.