概述
原文:http://www.cnblogs.com/yqskj/articles/2229480.html
最近经常见到这些unsigned char,short,int ,long的定义,有时真的不确定位长是多少
,上网查,又话是跟编译器有关,没一个准确答案。于是只好自己找答案,其实用一个最简
单地方就可以知道其长度,就是用sizeof函数。于是我写了如下一段:
unsigned char uint8 = 0;
signed char int8 = 0;
unsigned short uint16 = 0;
signed short int16 = 0;
unsigned int uint32 = 0;
signed int int32 = 0;
float fp32 = 0;
double fp64 = 0;
unsigned long ulong = 0;
printf("unsigned char is %d bitnr", sizeof(uint8)*8);
printf("signed char is %d bitnr", sizeof(int8)*8);
printf("unsigned short is %d bitnr", sizeof(uint16)*8);
printf("signed short is %d bitnr", sizeof(int16)*8);
printf("unsigned int is %d bitnr", sizeof(uint32)*8);
printf("signed int is %d bitnr", sizeof(int32)*8);
printf("float fp32 is %d bitnr", sizeof(fp32)*8);
printf("double fp64 is %d bitnr", sizeof(fp64)*8);
printf("unsigned long is %d bitnr", sizeof(ulong)*8);
复制代码编译运行后,答案出现了:
unsigned char is 8 bit
signed char is 8 bit
unsigned short is 16 bit
signed short is 16 bit
unsigned int is 32 bit
signed int is 32 bit
float fp32 is 32 bit
double fp64 is 64 bit
unsigned long is 32 bit
复制代码(上述结果是在arm-gcc编译得出)
令我感到意外的是,原来以为unsigned long是64位的,而其实它是32位而已,千万别被long
这个单词迷惑了。
不过,我又感到迷惑的是unsigned int 和unsigned long既然都是32位,那它们有什么区别
呢?
答案找到了:
在32位系统中
Long int = int = 32bit short int = 16bit
在16位系统中
short int = int = 16bit long int = 32bit
总之: short int = 16bit long int = 32bit
解决可移植性
最后
以上就是紧张飞机为你收集整理的谈谈unsigned char,short,int ,long的位长的全部内容,希望文章能够帮你解决谈谈unsigned char,short,int ,long的位长所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复