C语言各类型在x86与x64环境下的长度

本文探讨了C语言中的各种基本数据类型在x86和x64架构下的长度差异,包括int、long、char等类型的字节大小,分析了不同平台下编译器如何处理这些数据类型,帮助读者理解跨平台编程时需要注意的细节。
struct T {
    char a;
    int b;
    char c;
};

struct E {
};

// Linux 平台 (基于 CentOS6.5)
printf("%d\n", sizeof(struct T));     //x86 12  x64 12
printf("%d\n", sizeof(struct E));     //x86 0   x64 0
printf("%d\n", sizeof(char));         //x86 1   x64 1   
printf("%d\n", sizeof(short int));    //x86 2   x64 2   
printf("%d\n", sizeof(int));          //x86 4   x64 4   
printf("%d\n", sizeof(long));         //x86 4   x64 8   
printf("%d\n", sizeof(long long));    //x86 8   x64 8   
printf("%d\n", sizeof(float));        //x86 4   x64 4   
printf("%d\n", sizeof(double));       //x86 8   x64 8   
printf("%d\n", sizeof(long double));  //x86 12  x64 16  
printf("%d\n", sizeof(void));         //x86 1   x64 1   
printf("%d\n", sizeof(void*));        //x86 4   x64 8   
printf("%d\n", sizeof(size_t));       //x86 4   x64 8   

// Windows 平台 (基于 VS2013 Win10)
printf("%d\n", sizeof(struct T));     //x86 12  x64 12
printf("%d\n", sizeof(struct E));     //x86 1   x64 1
printf("%d\n", sizeof(short int));    //x86 2   x64 2
printf("%d\n", sizeof(int));          //x86 4   x64 4
printf("%d\n", sizeof(long));         //x86 4   x64 4
printf("%d\n", sizeof(long long));    //x86 8   x64 8
printf("%d\n", sizeof(float));        //x86 4   x64 4
printf("%d\n", sizeof(double));       //x86 8   x64 8
printf("%d\n", sizeof(long double));  //x86 8   x64 8
//printf("%d\n", sizeof(void));       //error C2070
printf("%d\n", sizeof(void*));        //x86 4   x64 8
printf("%d\n", sizeof(size_t));       //x86 4   x64 8
类型Linux x86Linux x64Win x86Win x64
struct T12121212
struct E0011
char1111
short int2222
int4444
long4844
long long8888
float4444
double8888
long double121688
void11--
void*4848
size_t4848

类型format
char%c
signed char%c (or %hhi for numerical output)
unsigned char%c (or %hhu for numerical output)
short
short int
signed short
signed short int

%hi
unsigned short
unsigned short int
%hu
int
signed
signed int

%i or %d
unsigned
unsigned int
%u
long
long int
signed long
signed long int

%li or %ld
unsigned long
unsigned long int
%lu
long long
long long int
signed long long
signed long long int

%lli or %lld
unsigned long long
unsigned long long int
%llu
float%f (promoted automatically to double for printf())
double%f (%F)(%lf (%lF) for scanf())
%g %G
%e %E
long double%Lf %LF
%Lg %LG
%Le %LE
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值