Online C language standard, 2011 draft:
6.4.4.4 Character constants
...
2 An integer character constant is a sequence of one or more multibyte characters enclosed
in single-quotes, as in 'x'
. A wide character constant is the same, except pre?xed by the
letter L
, u
, or U
. With a few exceptions detailed later, the elements of the sequence are
any members of the source character set; they are mapped in an implementation-de?ned
manner to members of the execution character set.
...
10 An integer character constant has type int
. The value of an integer character constant
containing a single character that maps to a single-byte execution character is the
numerical value of the representation of the mapped character interpreted as an integer.
The value of an integer character constant containing more than one character (e.g.,
'ab'
), or containing a character or escape sequence that does not map to a single-byte
execution character, is implementation-de?ned. If an integer character constant contains
a single character or escape sequence, its value is the one that results when an object with
type char
whose value is that of the single character or escape sequence is converted to
type int
.
So yes, in C, single character constants such as 'x'
have type int
.
Why this is the case is, AFAIK, largely lost to history1, although I suspect it was to minimize conversions when comparing against the results of getchar
(which returns an int
) or working with variadic functions like printf
(which would automatically promote any expressions of type char
to int
).
Since C++ provided alternate mechanisms for I/O and type-generic operations, it made sense for character constants to have type char
.
Does this matter in practice? Not in my experience, but my experience in text processing isn't that extensive.
I should actually read what I copy from the standard; the reason is given in that snippet. You can have multi-character constants like 'abc'
, which would not logically map to a single char
value.
1. Now watch sometbody find an extensive quote from Ritchie or Kernighan explaining exactly why they did it
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…