c - Little endian or Big endian -


#include <stdio.h>  union endian {     int i;     char c[sizeof(int)]; };  int main(int argc, char *argv[])  {     union endian e;     e.i = 1;     printf("%d \n",&e.i);     printf("%d,%d,\n",e.c[0],&(e.c[0]));     printf("%d,%d",e.c[sizeof(int)-1],&(e.c[sizeof(int)-1]));   } 

output:

1567599464  1,1567599464, 0,1567599467 

lsb stored in lower address , msb stored in higher address. isn't supposed big endian? system config shows little endian architecture.

you system little-endian.had been big-endian,the following code:

printf("%d,%d,\n",e.c[0],&(e.c[0])); 

will print 0 first %d instead of 1. in little-endian 1 stored as

00000001 00000000 00000000 00000000 ^ lsb ^lower address 

but in big-endian stored

00000000 00000000 00000000 00000001                            ^lsb                            ^higher address   

and don't use %d print addresses of variables, use %p.


Comments