K
Keith Thompson
Peter Nilsson said:Note that C++ didn't make 'A' a char to support overloading.
That is incidental. C++ made 'A' a char because it's common
sense to think of and _use_ 'A' as a character. The
situation is no different in C.
<OT>
According to the C++ standard, C++ made character constants type char
precisely because of overloading. See Annex C of the 1998 C++
standard:
Change: Type of character literal is changed from int to char
Rationale: This is needed for improved overloaded function
argument type matching.
The int-ness of 'A' may be surprising[*],
It is surprising.
Agreed (though I got over the surprise some time ago).
Can you think of any convention or practice elsewhere in
the language that would established a pattern with which
'A' being a character would be inconsistent?
Yes. 'A' is a constant. No other constants are of any integer type
smaller than int. For example, the macro CHAR_MAX typically expands
to either 127 or 255; both of these are constants of type int. The
language provides suffixes for integer constants that make them of
type long, unsigned int, unsigned long, and so forth, but there are no
such suffixes for char, unsigned char, signed char, short, or unsigned
short.
All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof". Why should character constants be a special case?
The only case where it makes any difference is when a character
constant is the operand of sizeof. How often do you really apply
sizeof to a character constant in real code (unless you're trying to
determine whether you're using a C or C++ compiler)?