Preprocessor code to figure out endianness ?

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Thu Sep 13 07:34:16 AEST 1990


In article <OTTO.90Sep9235213 at tukki.jyu.fi> otto at tukki.jyu.fi (Otto J. Makela) writes:
> I seem to remember seeing somewhere a real smart preprocessor construction
> to figure out the endianness of the machine under which the code was being
> compiled.  I cannot remember where and how, though ?  Any pointers ?

I don't know about this. I can never remember whether people mean
``big-endian'' as high-low or low-high; on the occasions when I need to
know byte order, I use something like

int hilo_magic = 1212958796; char *hilo = (char *) &hilo_magic;

hilo[0]hilo[1] is "HL" on machines that place the high byte first and
"LH" on others. It even works on sixteen-bit machines, provided the
compiler reduces 1212958796 to 18508.

---Dan



More information about the Comp.lang.c mailing list