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 x86 | Linux x64 | Win x86 | Win x64 |
|---|---|---|---|---|
| struct T | 12 | 12 | 12 | 12 |
| struct E | 0 | 0 | 1 | 1 |
| char | 1 | 1 | 1 | 1 |
| short int | 2 | 2 | 2 | 2 |
| int | 4 | 4 | 4 | 4 |
| long | 4 | 8 | 4 | 4 |
| long long | 8 | 8 | 8 | 8 |
| float | 4 | 4 | 4 | 4 |
| double | 8 | 8 | 8 | 8 |
| long double | 12 | 16 | 8 | 8 |
| void | 1 | 1 | - | - |
| void* | 4 | 8 | 4 | 8 |
| size_t | 4 | 8 | 4 | 8 |
| 类型 | 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 |
本文探讨了C语言中的各种基本数据类型在x86和x64架构下的长度差异,包括int、long、char等类型的字节大小,分析了不同平台下编译器如何处理这些数据类型,帮助读者理解跨平台编程时需要注意的细节。
3487





