C program to check Endianness of processor


From previous post we know how important it is to know Endianness of processor. I was searching methods to find it out.

I found that a simple C program will do that. let me tell you How?

Endianness affects the order in which data is stored..So Data types in C also are influenced by this feature of processor..

Important data types to be considered in Porting problem(Little-endian to Big-Endian and vice-vesra) are:

Unions(in turn structures), Byte arrays,Bit fields and pointer casting..

So we can exploit this property to serve our purpose..!!

#include<stdio.h>

#include<string.h>

int main (int argc, char* argv[]) {
FILE* fp;

/* Our example data structure */
struct {
char one[4];
int  two;
char three[4];
} data;

/* Fill our structure with data */

strcpy (data.one, “foo”);
data.two = 0x12345678;
strcpy (data.three, “bar”);

/* Write it to a file */
fp = fopen (“output”, “wb”);
if (fp) {
fwrite (&data, sizeof (data), 1, fp);
fclose (fp);
}
}
// copy paste this code and run with following commands

$ gcc -o endian endian.c

$ ./endian

$ hexdump -C output

00000000 66 6f 6f 00 78 56 34 12 62 61 72 00      |foo.xV4.bar.|

0000000c

Mine is intel duo core processor i386 processor..It’s little-Endian.

If you are running on big-endian processor you should get in 12 34  56 78 and darkened  |foo.xV4.bar.|  be in different order but with same characters..

Just a little more to know to about data types Endianness problems:

Data types :

Unions
A union is a variable that may hold objects of different types and sizes, with the compiler keeping track of the size and alignment requirements. Objects of dissimilar types and sizes can only be held at different times. A union provides a way to manipulate different kinds of data in a single area of storage.
Problem – Unions work fine for using the same memory to access different data. The key is to know what type of data exists in the memory before it is accessed. Accessing the same data with different types is not a valid use of unions and can cause endian issues.
Problem – If data types longer than 8 bits are united with a byte array, the data becomes byte order dependent.

Solution A – Don’t access the same data in memory as different data types.
Byte Arrays:
Byte Arrays – A character array that is used to hold a specified number of bytes. The size of array is always
equal to the number of bytes to hold.
Problem – If data in the byte array is accessed outside of its native data type, the data becomes byte order
dependent.
Example: An array that is initialized with a list of characters will be read as different values between little
endian and big endian platforms. The following example shows a byte array initialized to a,b,c,d. Accessing
this array as a long data type on a little endian platform will result in the value 0x64636261. On a big endian
platform it results in the value 0x61626364.
Solution – Avoid accessing byte arrays outside of the byte data type.
Bit Fields and Bit Masks
Bit operations are endian-sensitive. Even a bit field defined within a single byte is endian-sensitive. Code that
defines bit fields is subject to Endianness conflicts when porting the code to an opposite endian platform.

Pointer Casts
Casting pointers changes the native meaning of the original data. Doing so will affect which data is addressed.
Problem – If the native data pointer is a 32-bit pointer and is cast to a byte pointer, depending on the Endian-
architecture of the host, either the first byte or the last byte will be pointed to.
Example: Casting a pointer that stores the 32-bit value 0x11223344 to a byte pointer, the big-Endian system
points to 0x11. The little-Endian system points to 0x44.
Solution – Never change the native type of a pointer. Instead, get the data in its native data type format and
use byte swapping macros to access the bytes individually.

Enjoy Coding and Computing..:)

References:

http://www.ibm.com/developerworks/aix/library/au-endianc/index.html?ca=drs-

www.intel.com/design/intarch/papers/endian.pdf