1 #include <stdio.h> 2 int main() 3 { 4 int a; 5 long int b; 6 short int c; 7 char d; 8 signed char e; 9 unsigned char f; 10 double g; 11 float h; 12 int i[5];//int[5]型的数组,长度是int数据长度*5 13 printf("sizeof(int) =%d ",sizeof a); 14 printf("sizeof(long int) =%d ",sizeof b); 15 printf("sizeof(short int) =%d ",sizeof c); 16 printf("sizeof(char) =%d ",sizeof d); 17 printf("sizeof(signed char) =%d ",sizeof e); 18 printf("sizeof(unsigned char)=%d ",sizeof f); 19 printf("sizeof(double) =%d ",sizeof g); 20 printf("sizeof(float) =%d ",sizeof h); 21 printf("sizeof(g+g) =%d ",sizeof (g+g));//同类型相加,对类型无影响 22 printf("sizeof(int i[5]) =%d ",sizeof (i));//注意要写i 23 printf("i数组有%d个元素",sizeof(i)/sizeof(i[0]));//可以利用总长度/单个元素的长度求元素总数量 24 return 0; 25 }
结果:
sizeof(int) =4 sizeof(long int) =4 sizeof(short int) =2 sizeof(char) =1 sizeof(signed char) =1 sizeof(unsigned char)=1 sizeof(double) =8 sizeof(float) =4 sizeof(g+g) =8 sizeof(int i[5]) =20 i数组有5个元素