Alex Rivera | Logout

Determining endianness at compile time

Asked 2010-11-21T19:55:25.493
78

Is there a safe, portable way to determine (during compile time) the endianness of the platform that my program is being compiled on? I'm writing in C.

[EDIT] Thanks for the answers, I decided to stick with the runtime solution!

Edit
Report

1 Answer

11

Interesting read from the C FAQ:

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.

answered 2010-11-21T20:03:32.930

Your Answer