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位编译器
void 0 0 0
char 1 1 1
char * 2 4 8
short int 2 2 2
int 2 4 4
float 4 4 4
double 8 8 8
long 4 4 8
long double 8 12 16
long long 8 8 8


顺便看了一下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

猜你喜欢

转载自blog.csdn.net/sinan1995/article/details/79577106
今日推荐