C语言笔记-数据类型长度
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\n",sizeof a); 14 printf("sizeof(long int) =%d\n",sizeof b); 15 printf("sizeof(short int) =%d\n",sizeof c); 16 printf("sizeof(char) =%d\n",sizeof d); 17 printf("sizeof(signed char) =%d\n",sizeof e); 18 printf("sizeof(unsigned char)=%d\n",sizeof f); 19 printf("sizeof(double) =%d\n",sizeof g); 20 printf("sizeof(float) =%d\n",sizeof h); 21 printf("sizeof(g+g) =%d\n",sizeof (g+g));//同类型相加,对类型无影响 22 printf("sizeof(int i[5]) =%d\n",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个元素