C - Basics Program输出是什么? [英] C - Basics Program what is the output?

查看:51
本文介绍了C - Basics Program输出是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

What is the size of  the following structure?
typedef  struct
{
   int ctrl1;
   char flag;
   char flag2;
   int ctrl2;
   int ctrl3;
} MYREG;


a)14
b)20
c)24
d)16

推荐答案

取决于许多因素,主要是您使用的编译器。

如果使用旧的16位编译器,则int为2个字节。

如果使用更现代的编译器,则int可以是4或8个字节(我还没有看到使用128位整数的C编译器)。



另外,它将取决于你的编译器选项 - 有些将默认打包结构,其他人不会。如果它们打包它,它可能取决于目标处理器和编译器效率 - 一些组合将两个字符压缩为连续的字节,并且接下来的两个字节中的下一个int,其他将要求每个int以4字节开始边界。



所以,编译它,并尝试它。你为你的配置想出一个明确的答案并不困难。
Depends on a number of factors, primarily which compiler you use.
If you use an old 16 bit compiler, then an int is 2 bytes.
If you use a more modern compiler, then an int can be 4 or 8 bytes (I''ve yet to see a C compiler that used 128 bit integers).

In addition, it will depend on your compiler options - some will "pack" the structure by default, others won''t. If they do pack it, it may depend on the target processor and the compiler efficiency - some combinations will compact the two characters to successive bytes, and the following int into the next two bytes, others will require each int to start on a 4 byte boundary.

So, Compile it, and try it. It isn''t difficult for you to come up with a definitive answer for your configuration.


这篇关于C - Basics Program输出是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆