2.58 Check Little-Endian

★★

Problem:

Write a procedure is_litle_endian that will return 1 when compiled and run on a little-endian machine, and will return 0 when compiled and run on a big-endian machine. This program should run on any machine, regardless of its word size.

code:

#include <stdio.h>
#include <assert.h>

typedef unsigned char* byte_pointer;

int is_little_endian() {
    int val = 0xff;
    byte_pointer pval= (byte_pointer)&val;
    return pval[0]==0xff;
}

int main() {
    assert(is_little_endian());
    return 0;
}

Last updated