变量类型的具体大小 [英] Specific sizes of variable types

查看:75
本文介绍了变量类型的具体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在编写一个应用程序,它将打开二进制文件

整数的确定大小(2字节整数和4字节长)。有没有

a便携式方式确保每个整数都正好是2个字节?

我应该有一个平台相关的头文件来检查

CHAR_BIT和sizeof(int)等有几个typedef,如int16,long32

等。

< OT>

如果我有如果不便携,那么在编译之前确定

尺寸的方法是什么?相应的是>

< / OT>


谢谢

Hi all,

I''m writing an application which will be opening binary files with
definite sizes for integers(2 byte integers and 4 byte longs). Is there
a portable way in being sure that each integer will be exactly 2 bytes?
Should I have a platform dependent header file which checks for
CHAR_BIT and sizeof(int) etc and have a few typedefs like int16, long32
etc.
<OT>
If I have to go non-portable, then what''s the way to determine the
sizes before compilation and typedef accordingly?
</OT>

Thanks

推荐答案



" gamehack" < GA ****** @ gmail.com>在消息中写道

news:11 ********************* @ g49g2000cwa.googlegro ups.com ...

"gamehack" <ga******@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
大家好,

我正在编写一个应用程序,它将打开具有明确大小的二进制文件(2字节整数和4字节长)。是否有一种可移植的方式来确保每个整数将正好是2个字节?


您可以找到类型的大小,但不能更改它们。

请参阅''sizeof''运算符。

我是否应该有一个平台相关的头文件来检查
CHAR_BIT


你已经有了这样的标题作为你的实现的一部分。

(< limits.h>)。 CHAR_BIT给出一个字符中的位数

(字节)。 ''INT_MAX''为类型

''int''等提供最大可能值。

和sizeof(int)等


''sizeof''将给出特定类型

使用的字节数。


?并且有一些像int16,long32等类型的定义。
< OT>
如果我必须去非便携式,那么在编译之前确定
大小的方法是什么?相应的typedef?
Hi all,

I''m writing an application which will be opening binary files with
definite sizes for integers(2 byte integers and 4 byte longs). Is there
a portable way in being sure that each integer will be exactly 2 bytes?
You can find out the sizes of types, but you can''t change them.
See the ''sizeof'' operator.
Should I have a platform dependent header file which checks for
CHAR_BIT
You already have such a headers as part of your implementation.
(<limits.h>). CHAR_BIT gives the number of bits in a character
(byte). ''INT_MAX'' gives the largest possible value for type
''int'', etc.
and sizeof(int) etc
''sizeof'' will give the number of bytes a particular type
uses.

? and have a few typedefs like int16, long32 etc.
<OT>
If I have to go non-portable, then what''s the way to determine the
sizes before compilation and typedef accordingly?




编译之前的唯一方法是阅读你的文档。

在编译时,使用''sizeof''运营商。例如。你可以

对''fread()''''size''参数使用''sizeof''表达式。


C99实现可以提供精确大小的类型,但是你还需要检查文档,看看是否有/ b
可用于给定的实现。


-Mike



The only way before compilation is to read your documentation.
At compile-time, use the ''sizeof'' operator. E.g. you can
Use a ''sizeof'' expression for the ''size'' argument of ''fread()''.

C99 implementations can provide exact-sized types, but you''ll
still need to check documentation to see if/which one(s) are
available for a given implementation.

-Mike


" gamehack" < GA ****** @ gmail.com>写道:
"gamehack" <ga******@gmail.com> writes:
我正在编写一个应用程序,它将打开具有明确大小的二进制文件(2字节整数和4字节长整数)。是否有一种可移植的方式来确保每个整数都是2个字节?
我是否应该有一个平台相关的头文件来检查
CHAR_BIT和sizeof(int)等并且有一个很少有像int16,long32等的typedef
< OT>
如果我必须去非便携式,那么在编译之前确定
尺寸的方法是什么和typedef相应的?
< / OT>
I''m writing an application which will be opening binary files with
definite sizes for integers(2 byte integers and 4 byte longs). Is there
a portable way in being sure that each integer will be exactly 2 bytes?
Should I have a platform dependent header file which checks for
CHAR_BIT and sizeof(int) etc and have a few typedefs like int16, long32
etc.
<OT>
If I have to go non-portable, then what''s the way to determine the
sizes before compilation and typedef accordingly?
</OT>




C99有一个名为< stdint.h>的标准头文件,它提供了typedef

for int8_t,uint8_t,int16_t,uint16_t,int32_t等。这些是

精确宽度类型,签名的那些必须是

2'' s-complement没有填充位;如果

实现不提供具有所需属性的类型,则它们可能不存在。它

也提供最少类型(具有至少

指定位数的最小类型)和快速类型。类型(最快类型,其中

至少指定的位数);它并不总是很清楚

最快的是什么?真的意思。


如果你的实现没有提供这个标题,你可以滚动你的

拥有,或者你可以使用Doug Gwyn的C90兼容的公共领域

实施q8 at< http://www.lysator.liu.se/c/q8/>。你可能需要为你的系统做一些剪裁。


-

Keith Thompson(The_Other_Keith)< a href =mailto:ks *** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



C99 has a standard header called <stdint.h>, which provides typedefs
for int8_t, uint8_t, int16_t, uint16_t, int32_t, etc. Those are
exact-width types, and the signed ones are required to be
2''s-complement with no padding bits; they may not exist if the
implementation doesn''t provide types with the required attributes. It
also provides "least" types (the smallest type with at least the
specified number of bits) and "fast" types (the "fastest" type with at
least the specified number of bits); it''s not always clear what
"fastest" really means.

If your implementation doesn''t provide this header, you can roll your
own, or you can use Doug Gwyn''s C90-compatible public-domain
implementation q8 at <http://www.lysator.liu.se/c/q8/>. You''ll
probably need to do some tailoring for your system.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


谢谢


Mike Wahler写道:
Thanks

Mike Wahler wrote:
" gamehack" ; < GA ****** @ gmail.com>在消息中写道
新闻:11 ********************* @ g49g2000cwa.googlegro ups.com ...
"gamehack" <ga******@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
你好所有,

我正在编写一个应用程序,它将打开具有明确大小的二进制文件(2字节整数和4字节长)。是否有一种可移植的方式来确保每个整数都是2个字节?
Hi all,

I''m writing an application which will be opening binary files with
definite sizes for integers(2 byte integers and 4 byte longs). Is there
a portable way in being sure that each integer will be exactly 2 bytes?



你可以找出类型的大小,但你不能改变它们。
请参阅''sizeof''运算符。



You can find out the sizes of types, but you can''t change them.
See the ''sizeof'' operator.

我是否应该有一个平台相关的头文件来检查
CHAR_BIT
Should I have a platform dependent header file which checks for
CHAR_BIT



您已经有了这样的标题作为实现的一部分。
(< limits.h>)。 CHAR_BIT给出字符中的位数
(字节)。 ''INT_MAX''为类型
''int''等提供了最大可能的值。



You already have such a headers as part of your implementation.
(<limits.h>). CHAR_BIT gives the number of bits in a character
(byte). ''INT_MAX'' gives the largest possible value for type
''int'', etc.

和sizeof(int)etc
and sizeof(int) etc



''sizeof''将给出特定类型使用的字节数。

?并且有几个typedef,如int16,long32



''sizeof'' will give the number of bytes a particular type
uses.

? and have a few typedefs like int16, long32

等。
< OT>
如果我必须去非便携式,那么确定的方法是什么
编译前的大小和typedef相应吗?
etc.
<OT>
If I have to go non-portable, then what''s the way to determine the
sizes before compilation and typedef accordingly?



编译之前的唯一方法是阅读你的文档。
在编译时,使用''sizeof''运营商。例如。你可以对''fread()''的''size''参数使用''sizeof''表达式。

C99实现可以提供精确大小的类型,但是你'll
仍然需要检查文档,看看是否有/哪一个可用于给定的实现。

-Mike



The only way before compilation is to read your documentation.
At compile-time, use the ''sizeof'' operator. E.g. you can
Use a ''sizeof'' expression for the ''size'' argument of ''fread()''.

C99 implementations can provide exact-sized types, but you''ll
still need to check documentation to see if/which one(s) are
available for a given implementation.

-Mike






这篇关于变量类型的具体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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