面试题,c语言 struct 结构体有 int short byte 占用多少内存?
面试问题:
c语言 struct 结构体有 int short byte 共占用多少字节内存?
1.Windows + VC6.0++ 环境结果:
代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct t { 5 int a; //4 6 short b; //2 7 char c; //1 8 //byte d; 9 }t1; 10 11 int main() { 12 int a; 13 a=sizeof(t1); 14 15 printf("t1 sizeof = %d\n", a); 16 exit(0); 17 }
注: C中没有内定 byte 类型,用 char 代替,但 char 不完全等于 byte。 详细说明
结果:

2. CentOS 7 + gcc 4.8.5
1 #include <stdio.h> 2 3 struct t { 4 int a; 5 short b; 6 char c; 7 }t1; 8 9 main() { 10 int a; 11 a=sizeof(t1); 12 13 printf("t1 sizeof=%d\n", a); 14 15 }
结果:

3. Ubuntu 22.04 + gcc 11.3
代码:
1 #include <stdio.h> 2 3 struct t { 4 int a; 5 short b; 6 char c; 7 }t1; 8 9 void main() { 10 int ss; 11 ss = sizeof(t1); 12 printf("t1 sizeof=%d\n",ss); 13 }
结果:

-
浙公网安备 33010602011771号