In C, the character constant is still of type int, not char like in C++.
Multi-character constants are possible, and have an implementation-defined value.
int fourcc = 'abcd';
(This may be supported by C++ also, I'm not sure. So that is to say, a multi-character constant in C++ perhaps doesn't have type char, but an implementation-defined type.)
GCC on Ubuntu 18.04:
$ cat hello.c
#include <stdio.h>
int main(void)
{
int hello[] = { 'lleH', 'w ,o', 'dlro', '!', 0 };
puts((char *) hello);
return 0;
}
$ gcc hello.c -o hello
hello.c: In function ‘main’:
hello.c:5:19: warning: multi-character character constant [-Wmultichar]
int hello[] = { 'lleH', 'w ,o', 'dlro', '!', 0 };
^~~~~~
[ ... and similar errors ...]
$ ./hello
Hello, world!
It works if the program is compiled with g++ also, in spite of a single character constant like '!' being char.
We have to write the characters backwards because of little endian. In the source code, the leftmost character is the most significant byte, and on the little-endian system, that means it goes to the higher address. The first character H must be the rightmost part of the character constant so that it ends up in the least significant byte which goes to the lowest address in memory.
Endianness wouldn't be an issue in B because it doesn't exist; there is no "char *" pointer accessing the data as individual characters. B could be implemented on big or little endian and the string handling would work right.
Multi-character constants are possible, and have an implementation-defined value.
(This may be supported by C++ also, I'm not sure. So that is to say, a multi-character constant in C++ perhaps doesn't have type char, but an implementation-defined type.)GCC on Ubuntu 18.04:
It works if the program is compiled with g++ also, in spite of a single character constant like '!' being char.We have to write the characters backwards because of little endian. In the source code, the leftmost character is the most significant byte, and on the little-endian system, that means it goes to the higher address. The first character H must be the rightmost part of the character constant so that it ends up in the least significant byte which goes to the lowest address in memory.
Endianness wouldn't be an issue in B because it doesn't exist; there is no "char *" pointer accessing the data as individual characters. B could be implemented on big or little endian and the string handling would work right.