Unfortunately (imho) char
is considered an integer type and you can treat it as an integer type without any explicit cast.
char
is a different type than signed char
and unsigned char
. Whether char
is unsigned or signed is implementation defined.
When used in arithmetic operations (including comparisons) integer types with rank less or equal to rank of int
undergo integer promotion so your code is equivalent with:
if ((int)i < (int)c)
Another use for char
is to access raw memory. C
doesn't have a byte type and char
is ... the byte type.
Just because you can doesn't mean you should. Use char
for ... well ... characters. For memory access a lot of API uses char*
, but if you can you should use unsigned char*
. For small integers if you really need to save the space use int8_t
and uint8_t
which should be aliases for signed char
and unsigned char
respectively.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…