In C, a char
is an integer type (like int
, and long long int
).
It functions just like the other integer types, except the range of values it can store is typically limited to -128 to 127, or 0 to 255, although this depends on implementation.
For example:
char x = 3;
char y = 6;
int z;
z = x + y;
printf("z = %d\n", z); //prints z = 9
The char
type (usually as part of an array) is most often used to store text, where each character is encoded as a number.
Character and string constants are a convenience. If we assume the machine uses the ASCII character set (which is almost ubiquitous today), in which case capital A is encoded as 65, then:
char x = 'A';
char str[] = "AAA";
is equivalent to
char x = 65;
char str[] = {65, 65, 65, 0};
Therefore, something like 'X' + 6
makes perfect sense - what the result will be depends on the character encoding. In ASCII, it's equivalent to 88 + 6
which is 94
which is '^'
.