C语言数据类型占用字节大小

在昨天的笔试的时候碰到了一个关于不同的数据类型占用字节大小的问题,于是就想归纳整理一下关于这方面的问题。于是就写了一下,在Linux系统下用gcc编译验证了一下,以供参考。

16位编译器:

char/unsigned char :1字节
char *:2字节
short int:2字节
int/unsigned int:2字节
long int:4字节
float:4字节
double:8字节

32位编译器:
char/unsigned char :1字节
char *:4字节
short int:2字节
int/unsigned int:4字节
long int:4字节
float:4字节
double:8字节
long long:8字节
long double:12字节

64位编译器:
char/unsigned char :1字节
char *:8字节
short int:2字节
int/unsigned int:4字节
long int:8字节
float:4字节
double:8字节
long long:8字节

long double:16字节


在上面我们也可以看到不管16/32/64位的编译器,char、short int、float、double、long long这几个类型占用的字节数都是相同的,比较特殊的是long double在32位编译器下占用的字节是12位。

类型/编译器16位编译器32位编译器64位编译器
void000
char111
char *248
short int222
int244
float444
double888
long448
long double81216
long long888


顺便看了一下unsigned long int和unsigned int的区别:

在32位编译器下,因为同占4字节,因此没有取值范围没有区别


在64位编译器下,unsigned long int占8字节,而unsigned int占4字节,就有区别了



由于我安装的Ubuntu是64位版本的,因此想要生成32位的程序,需要安装下面的库

$ sudo apt-get install build-essential module-assistant  
$ sudo apt-get install gcc-multilib g++-multilib


版权声明:本文为sinan1995原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。