Wednesday, July 23, 2014

Find out your machine is little endian or big endian by c program - linux

There are lots of ways available on net to find out machine is little endian or big endian.

But the way I am showing is to find out endianess by program that takes optimum memory.

The program will find it just by 2 bytes, as follow

#include <stdio.h>
#include <inttypes.h>

struct abc {
    union {
        int16_t a;
        char c[sizeof(int16_t)];
    };
};

int
main() {
    struct abc abc;
    abc.a = 1;

    if(abc.c[0] == 1) {
        printf("Machine is little endian.\n");
    } else {
        printf("Machine is big endian\n.");
    }

    return 0;
}


<Sumit Kakadiya>                                                                                                                                                                   

No comments:

Post a Comment