prev up next   top/contents search

comp.lang.c FAQ list · Question 10.16

Q: How can I use a preprocessor #if expression to tell whether a machine's byte order is big-endian or little-endian?


A: You probably can't. The usual techniques for detecting endianness involve pointers or arrays of char, or maybe unions, but preprocessor arithmetic uses only long integers, and there is no concept of addressing. Another tempting possibility is something like

	#if 'ABCD' == 0x41424344
but this isn't reliable, either. At any rate, the integer formats used in preprocessor #if expressions are not necessarily the same as those that will be used at run time.

Are you sure you need to know the machine's endianness explicitly? Usually it's better to write code which doesn't care (see for example the code fragment in question 12.42). See also question 20.9.

References: ISO Sec. 6.8.1
H&S Sec. 7.11.1 p. 225


prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North