Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find Endian-ness of my PC programmatically using C? [duplicate]

Tags:

c

endianness

Possible Duplicate:
Detecting endianness programmatically in a C++ program

Is there any library function available to find the endian-ness of my PC?

like image 220
Dinesh Avatar asked Dec 20 '11 05:12

Dinesh


People also ask

How do you check endianness of a computer?

In case we need to deal with endianness, there are ways to detect and deal with the difference of endianness at run time using a simple program logic or utilities that are provided such as htonl, htons, ntohl, ntohs or std::endian.

Is there a quick way to determine endianness of your machine?

We can also check the endianness of the machine using the union. We need to create a union that has an integer variable and an array of 4 characters. If the first element (au8DataBuff [0]) of the character array is equal to the LSB Bytes of integer, then the system will be little endian otherwise big-endian.

Is my PC Little Endian?

If you are not sure which endian your machine is, you can conduct this test from within IDL: little_endian = (BYTE(1, 0, 1))[0] IF (little_endian) THEN $ Print, "I'm little endian." ELSE $ Print, "I'm big endian."

What is endianness of the CPU?

Endianness is the term used to describe the byte order of data. Big-endian means data is being sent or stored with the Most Significant Byte (MSB) first. Little-endian means data is sent with the Least Significant Byte (LSB) first. A CPU always has an endianness, even for 8 bit CPUs.


1 Answers

Why you need a library if you can find it like this? :)

int num = 1;

if (*(char *)&num == 1)
{
    printf("Little-Endian\n");
}
else
{
    printf("Big-Endian\n");
}
like image 153
COD3BOY Avatar answered Sep 29 '22 04:09

COD3BOY