The below program tests for Little/Big endian on intel processor. Actually little endian is correct output. First I am casting int
to char*
and accessing its value without initialization to int *
.I am not understanding second part of output. Here int
pointer is casted to char *
. So why is not int
pointer not changed its alignment to char *
?
00000000 00000000 00000011 01111111 = 895
0 0 3 127
int main() {
int num = 895;
if(*(char *)&num == 127)
{
printf("\nLittle-Endian\n");
}
else
{
printf("Big-Endian\n");
}
int *p = (char *)&num ;
if(*p == 127)
{
printf("\nLittle-Endian\n");
}
else
{
printf("Big-Endian\n");
}
printf("%d\n",*p);
}
o/p
Little-Endian
Big-Endian
895