现在的位置: 首页 > 综合 > 正文

用关键字union确认当前系统的存储模式

2012年11月25日 ⁄ 综合 ⁄ 共 264字 ⁄ 字号 评论关闭

存储模式分为两种:
大端模式(Big_endian):数据的高字节存储在低地址中,低字节存储在高地址中。
小端模式(Little_endian):数据的高字节存储在高地址中,低字节存储在低地址中。

#include <stdio.h>
#include <malloc.h>

// 返回1则为小端存储,否则为大端存储
int checkSystem()
{
    union check
    {
        int i;
        char ch;
    }c;
    c.i = 1;
    return (c.ch == 1);
}

int main()
{
    printf("%d\n", checkSystem());

    return 0;
}

抱歉!评论已关闭.