F
Frederick Gotham
The "toupper" function takes an int as an argument. That's not too
irrational given that a character literal is of type "int" in C.
(Although why it isn't of type "char" escapes me... )
The "toupper" function imposes a further constrait in that the value
passed to it must be representable as a unsigned char. (If C does not
require all character values to be positive, then again, this constrait
too escapes me... )
Let's say we have the following hypothetical system:
char is signed.
UCHAR_MAX == 255
SCHAR_MAX == 127
CHAR_MAX == 127
INT_MAX == 65535
We are able to represent all the characters of ASCII using positive
numbers, but anything beyond that would require negative numbers on this
system.
So what's the deal with using toupper on these extraneous characters
whose numeric value is negative?
Let's say we have a German sharp S, or a Spanish N with a curly thing on
top of it, and that its numeric value is negative. How do we go about
passing their value to toupper? Should we do the following?
toupper( (unsigned char)c );
(One more thing. If you have a signed integer value, and you cast it to
its corresponding unsigned integer type, and then back to the signed
type, are you guaranteed to have the same value? i.e.:
signed char s = -5;
unsigned char us = s;
s = us;
assert( -5 == s ); /* Is this guaranteed? */
irrational given that a character literal is of type "int" in C.
(Although why it isn't of type "char" escapes me... )
The "toupper" function imposes a further constrait in that the value
passed to it must be representable as a unsigned char. (If C does not
require all character values to be positive, then again, this constrait
too escapes me... )
Let's say we have the following hypothetical system:
char is signed.
UCHAR_MAX == 255
SCHAR_MAX == 127
CHAR_MAX == 127
INT_MAX == 65535
We are able to represent all the characters of ASCII using positive
numbers, but anything beyond that would require negative numbers on this
system.
So what's the deal with using toupper on these extraneous characters
whose numeric value is negative?
Let's say we have a German sharp S, or a Spanish N with a curly thing on
top of it, and that its numeric value is negative. How do we go about
passing their value to toupper? Should we do the following?
toupper( (unsigned char)c );
(One more thing. If you have a signed integer value, and you cast it to
its corresponding unsigned integer type, and then back to the signed
type, are you guaranteed to have the same value? i.e.:
signed char s = -5;
unsigned char us = s;
s = us;
assert( -5 == s ); /* Is this guaranteed? */