找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 337|回复: 3

Question1.3Since C doesn't define sizes exactly, I've been using typedefs like int16 and int

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-6-26 21:32:29 | 显示全部楼层 |阅读模式
If you truly need control over exact type sizes, this is the right approach.There remain several things to be aware of:

  • There might not be an exact match on some machines. (There are, for example, 36-bit machines.)
  • A typedef like int16 or int32 accomplishes nothing if its intended meaning is ``at least'' the specified size, because types int and long are already essentially defined as being ``at least 16 bits'' and ``at least 32 bits,'' respectively.
  • Typedefs will never do anything about byte order problems (e.g. if you're trying to interchange data or conform to externally-imposed storage layouts).
  • You no longer have to define your own typedefs, because the Standard header <inttypes.h> contains a complete set.

See also questions 10.16 and 20.5.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-6-27 10:41:54 | 显示全部楼层
问:由于C不能精确定义类型的大小,我使用typedefs定义int16和int32。然后我就可以根据我所使用的机器,用typedefs来定义int、short、long等等。那么这样所有的问题就可以迎刃而解了。
回答:如果你真的想精确的控制这些类型的大小,上述问题中描述的确是一个很好的方法。但还需要注意几点:
         在某些机器上可能没有准确的搭配。(比如说36位机器)
         如果typedefs定义int16和int32只是为了表明”至少“这么长,则没有实际意义。因为,int和long类型已经被分别定义为“至少16位”和“至少32位”。
         typedefs定义对于字节序的问题将不会有任何作用(例如:当你正试图交换数据或者遵循外部强加的存储布局时)。
         你不用做自己的typedefs定义,因为在标准头文件<inttypes.h>中包含了完整的设置。
参考问题10.16和20.5。

14

主题

65

回帖

0

积分

新手上路

积分
0
发表于 2013-6-28 01:03:23 | 显示全部楼层
还没见过36位的机器。
翻译得很好,赞一个!

205

主题

173

回帖

6925

积分

论坛元老

积分
6925
发表于 2013-6-28 11:58:02 | 显示全部楼层
很不错,继续努力哦
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 12:26 , Processed in 0.084918 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表