计算机存储数据(如一个int型)时分为大端存储和小端存储两种方式;小端存储方式为低地址存储低字节数据,高地址存储高字节数据。大端存储方式相反, 即:低地址存储高字节数据,高地址存储低字节数据。
小端存储方式示意图(如下):
测试方法:1.定义一个包含int 和 char到联合体union 2.利用char*型指针
方法一
#include <stdio.h> #include <stdlib.h> int checkSystem() { union check { int i; char ch; }c; c.i = 1; return (c.ch == 1); //c.ch如果等于1,则返回1,否则返回0; } //返回1说明该计算机是小端存储,相反是大端存储 int main() { int a = checkSystem(); printf("%d\n", a); system("pause"); return 0; }
方法二
#include <stdio.h> #include <stdlib.h> int checkSystem() { int v = 1; if (*((char *)&v) == 1) return 1; //返回1说明是小端存储 else return 0; //返回0说明是大端存储 } int main() { int a = checkSystem(); printf("%d\n", a); system("pause"); return 0; }
本文出自 “木木侠” 博客,谢绝转载!
原文:http://10324228.blog.51cto.com/10314228/1709781