C语言判断系统数据大/小端存储方式

时间:2014-05-17 21:03:42   收藏:0   阅读:394

    小端存储:数据的低位部分,存储于存储器的低地址空间里。

    大端存储:数据的低位部分,存储于存储器的高地址空间里。

    首先,一般PC数据存储方式是小端存储。

    基本实现思想是:将存储器中所存的数据按字节以地址顺序输出,与存入数据的高低位进行比较,即得出结论。

实现方法一:

bubuko.com,布布扣
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     short int x;
 5     char *arr;
 6     
 7     x = 0x1122;  
 8     arr = (char *)&x;
 9     
10     if(arr[0]==0x22)
11         printf("The compute is little-endian.\n");
12     else if(arr[0]==0x22)
13         printf("The compute is big-endian.\n");
14     getchar();
15     return 0;    
16 }
bubuko.com,布布扣

实现方法二:

bubuko.com,布布扣
 1 #include <stdio.h>
 2 
 3 union data
 4 {
 5     int inter;
 6     char ch;    
 7 };
 8 
 9 int main(void)
10 {
11     union data c;
12     c.inter = 1;
13     if(c.ch == 1)
14         printf("The compute is little-endian.\n");
15     else
16         printf("The compute is big-endian,\n");
17         
18     getchar();
19     return 0;    
20 }
bubuko.com,布布扣

 

C语言判断系统数据大/小端存储方式,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!