Archived

This topic is now archived and is closed to further replies.

Compile time byte order constant

This topic is 5511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

is there a standard C or C++ file that defines a constant to determine Least-Significant / Most significant byte orderings of the target system?

Share on other sites
No. Generally, you determine your target machines ahead of time and then use conditional compilation to compile in the correct code. It is up to the person building the code to specify the correct target machine at compile time.

That said, it''s fairly easy to determine byte ordering at run-time (assign a number to a variable and check the value of the first byte). But, to make use of that information, your application would be full of if-statements and duplicate code and it probably wouldn''t be worth it.

Share on other sites
Although this may not be useful for you, some unixes define their architecture''s endian style. A simple demonstration:
#include <stdio.h>#include <sys/param.h>int main(void) {	#if defined(BYTE_ORDER)		#if BYTE_ORDER == LITTLE_ENDIAN			fputs("little endian\n",stdout);		#else			fputs("big endian\n",stdout);		#endif	#elif defined(__BYTE_ORDER)		#if __BYTE_ORDER == __LITTLE_ENDIAN			fputs("little endian\n",stdout);		#else			fputs("big endian\n",stdout);		#endif	#else		fputs("unknown endian\n",stdout);	#endif	return 0;}

This should work in most or all Linux distro''s. I think this works in FreeBSD, but I can''t remember (and I can''t check at the moment). This doesn''t work in Solaris 8, but may in other versions. But, since this is normally used for *nix stuff, you would normally back it up with a configure script to check the endian order for you before compiling.