On my machine, the following program writes 1234 to it’s output.
const char str[] = "1234";
printf("%c%c%c%c\n",
(int) (0xff & (*(uint32_t*) str) >> 0),
(int) (0xff & (*(uint32_t*) str) >> 8),
(int) (0xff & (*(uint32_t*) str) >> 16),
(int) (0xff & (*(uint32_t*) str) >> 24));
This implies that str is internally represented as 0x34333231, and the first byte str[0] represents the least significant 8 bits.
Does this mean str is encoded in little endian? And is the output of this program platform-dependent?
Also, is there a convenient way to use 1, 2, 4 and 8 character string literals in switch case statements? I can’t find any way to convert the strings to integers, as *(const uint32_t* const) "1234" is not a constant expression, and 0x34333231/0x31323334 might be platform dependent and must be notated in hexadecimal.
edit:
In other words, is 0xff & *(uint32_t*) str always equal to str[0]?
Eh, never mind, just realized it is and also why.
Endianness refers to the order of bytes in a larger value. Strings are (at least in C and C++) an array of bytes so endianness doesn’t apply.
You actually can do what you mention in the last paragraph using multicharacter literals, though it’s implementation defined exactly how it works and the string must be no longer than
sizeof(int).For instance,
'abcd'is a value of typeintwith an implementation-defined value. This value probably would depend on endianness. Since it is an integer, you are allowed to switch on it.