Possible Duplicate:
How to check whether a system is big endian or little endian?
how to know whether the machine am using is big or little endian? Is there a code i can write to do that?
Possible Duplicate:
How to check whether a system is big endian or little endian?
how to know whether the machine am using is big or little endian? Is there a code i can write to do that?
int main()
{
unsigned int i = 0x12345678; // assuming int is 4 bytes.
unsigned char* pc = (unsigned char*) &i;
if (*pc == 0x12)
printf("Big Endian. i = 0x%x, *pc = 0x%x\n", i, *pc);
else if (*pc == 0x78)
printf("Little Endian. i = 0x%x, *pc = 0x%x\n", i, *pc);
return 0;
}
Following code should get you the answer.
#include <stdio.h>
int main() {
long x = 0x44434241;
char *y = (char *) &x;
if(strncmp(y,"ABCD",4)){
printf("Big Endian\n");
}else{
printf("little Endian\n");
}
}
Explanation
In little endian
4 bytes are stored as [4th, 3rd , 2nd, 1st]
. 0x41
is A
and 0x42
is B
and so. This bytestrem is interpreted as character string and using strncpy
we determine how bytes are actually arranged in machine and decide is its little or big endian
.