字节序

 

#include<stdio.h>
#include<cstdlib>

int main()
{
    union 
    {
        short s;
        char c[sizeof(s)];
    }un;

    un.s = 0x0102;
    if (sizeof(short) == 2)
    {
        if (un.c[0] == 1 && un.c[1] == 2)
        {
            printf("big-endina\n");
        }
        else if (un.c[1] == 1 && un.c[0] == 2)
        {
            printf("small-endina\n");
        }
        else
        {
            printf("unknown\n");
        }
        
    }
    else
    {
        printf("sizeof(short) = %d\n", sizeof(short));
    }

    return 0;    

}

 

posted @ 2020-06-27 09:58  王清河  阅读(146)  评论(0编辑  收藏  举报