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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.