C program to check little vs. big endian [duplicate]
In short, yes.
Suppose we are on a 32-bit machine.
If it is little endian, the x
in the memory will be something like:
higher memory -----> +----+----+----+----+ |0x01|0x00|0x00|0x00| +----+----+----+----+ A | &x
so (char*)(&x) == 1
, and *y+48 == '1'
.
If it is big endian, it will be:
+----+----+----+----+ |0x00|0x00|0x00|0x01| +----+----+----+----+ A | &x
so this one will be '0'
.
The following will do.
unsigned int x = 1;printf ("%d", (int) (((char *)&x)[0]));
And setting &x
to char *
will enable you to access the individual bytes of the integer, and the ordering of bytes will depend on the endianness of the system.
This is big endian test from a configure script:
#include <inttypes.h>int main(int argc, char ** argv){ volatile uint32_t i=0x01234567; // return 0 for big endian, 1 for little endian. return (*((uint8_t*)(&i))) == 0x67;}