I found the following code to check the byte order state. It works with VS C++:
#if '1234' != 0x31323334
# define BIG_ENDIAN
#endif
I don't know if it works with other compilers. I would ask if it is a standard code?
A character constant with more than one character, such as '1234'
, has an implementation-defined value. There is absolutely no guarantee about the relationship between the value of '1234'
and the system's endianness.
The preprocessor just doesn't have enough information to determine the target system's byte order.
And keep in mind that big-endian and little-endian aren't the only possibilities. For a 4-byte integer type, there are 24 (4 factorial) possible byte orderings. All of them are permitted by the language, and at least 3 of them have been used by real machines.