following are the ways to find machine is little endian or big endian, and how to verify my program is working fine?
# 1
/* program to find machine is little endian or little endian */
#include<stdio.h>
int main()
{
int x=0x12345678;
char *p;
p=(char *)&x;
if(*p == 0x78) {
printf("Little endian \n");
} else {
printf("Big endian \n");
}
return 0;
}
#2
/* another correct way is using unions */
int main()
{
union e {
int x;
char y;
};
union e p1;
p1.x=0x12345678;
if(p1.y == 0x78) {
printf("Little endian \n");
} else {
printf("Big endian \n");
}
return 0;
}
#3
below is same as #1 with more simplified
shiv@ubuntu:~/ds/misc$
/* following is not correct way to find the machine */
#include<stdio.h>
int main()
{
int num=1;
if(*(char *)&num == 1)
{
printf("Little\n");
} else {
printf("Big endian\n");
}
}
below is readelf utility we can verify our compiled binary is for little endian or bigendian?
so that what our program is answer match with readelf utility
# 1
/* program to find machine is little endian or little endian */
#include<stdio.h>
int main()
{
int x=0x12345678;
char *p;
p=(char *)&x;
if(*p == 0x78) {
printf("Little endian \n");
} else {
printf("Big endian \n");
}
return 0;
}
#2
/* another correct way is using unions */
int main()
{
union e {
int x;
char y;
};
union e p1;
p1.x=0x12345678;
if(p1.y == 0x78) {
printf("Little endian \n");
} else {
printf("Big endian \n");
}
return 0;
}
#3
below is same as #1 with more simplified
shiv@ubuntu:~/ds/misc$
/* following is not correct way to find the machine */
#include<stdio.h>
int main()
{
int num=1;
if(*(char *)&num == 1)
{
printf("Little\n");
} else {
printf("Big endian\n");
}
}
below is readelf utility we can verify our compiled binary is for little endian or bigendian?
so that what our program is answer match with readelf utility
Comments
Post a Comment