PETA e-cards

Friday, August 25, 2006

General C Questions

QUESTIONS:
(SCROLL DOWN FOR ANSWERS)

1) Checking the Endianness of a machine:
ANSWERS:


1) Checking the Endianness of a machine:

Solution a):

#include

int main(void)
{
int x=1;

if (*(char *)&x == 1)
{
printf("Little Endian\n");
}
else
{
printf("Big Endian\n");
}

return 0;
}

Solution b):

#include

int main(void)
{

union
{
int i;
char c[sizeof(int)];
}x;

x.i=1;

if (x.c[0] == 1)
{
printf("Little-Endian\n");
}
else
{
printf("Big-Endian\n");
}

return 0;
}