经典!判断大小端的程序

判断大小端的程序:

#include <stdio.h>

int is_little_endian_0()
{
    long  t;
    char *p;

    t = 0x1;
    p = (char *)&t;
    
    if (*p == 0x1) {
        // big endian
        return 0;
    }
    // little endian
    return 1;
}

int is_little_endian_1()
{
    union endian {
        long data;
        char bytes[sizeof(long)];
    };
    union endian d;
    d.data = 0x1;
    if (d.bytes[0] == 1) {
        // big endian
        return 0;
    }
    // little endian
    return 1;
}


void main()
{
    printf("is_little_endian_0() = %d.\n", is_little_endian_0());
    printf("is_little_endian_1() = %d.\n", is_little_endian_1());
}


你可能感兴趣的:(经典!判断大小端的程序)