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

时间:2022-10-13 01:00:31
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