面试题,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 }
复制代码

结果:

 

 

 

 

 

 

-

posted @   悟透  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2021-03-10 安装mysql5.7
点击右上角即可分享
微信分享提示