C 中的精确宽度整数类型 (stdint.h)

发布于 2024-10-15 20:24:44 字数 147 浏览 0 评论 0原文

我希望 char、short 和 int 类型的宽度为 1、2 和 4 个字节。我已将 stdint.h 标头包含到我的源代码中。这是否保证 int8_t、int16_t 和 int32_t 整数类型具有指定的宽度?实现这一目标的最佳方法是什么?

I would like char, short and int types to be 1, 2 and 4 bytes width. I have included a stdint.h header into my sources. Does this guarantee that int8_t, int16_t and int32_t integer types will be of widths specified? What is the best way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ζ澈沫 2024-10-22 20:24:44

如果这些类型存在,则它们具有正确的宽度并以二进制补码编码。

编辑:您应该能够检查类型是否存在,

#ifdef INT32_MAX
...
#endif

但在所有最终用户架构(PC 等)上,这些类型通常确实存在。

If these types exist, they have the correct width and are encoded in two's complement.

Edit: you should be able to check if the types exist with something like

#ifdef INT32_MAX
...
#endif

but on all end user architectures (PC etc) these types do usually exist.

盛夏尉蓝 2024-10-22 20:24:44

stdint.h 中定义的类型的全部目的是具有指定的宽度。因此,int8_t 将为 8 位宽,int16_t - 16 位宽,等等。

The whole purpose of the types defined in stdint.h is of them to be of specified width. So int8_t will be 8 bits wide, int16_t - 16 bits wide, etc.

回眸一笑 2024-10-22 20:24:44

stdint.h 一些精确宽度的整数类型。这些是(来自 wikipedia):

Specifier   Signing      Bits   Bytes   
int8_t        Signed       8       1    
uint8_t       Unsigned     8       1    
int16_t       Signed       16      2    
uint16_t      Unsigned     16      2    
int32_t       Signed       32      4    
uint32_t      Unsigned     32      4    
int64_t       Signed       64      8 
uint64_t      Unsigned     64      8    

如果您想知道尺寸的用途,请使用它们定。 C99 要求这些类型具有指定的大小。所有其他类型均取决于平台。

其他类型保证具有特定的最小宽度,根据 limits.h 。但它们同样可以比这更大。这取决于实施。

stdint.h some exact-width integer types. These are (from wikipedia):

Specifier   Signing      Bits   Bytes   
int8_t        Signed       8       1    
uint8_t       Unsigned     8       1    
int16_t       Signed       16      2    
uint16_t      Unsigned     16      2    
int32_t       Signed       32      4    
uint32_t      Unsigned     32      4    
int64_t       Signed       64      8 
uint64_t      Unsigned     64      8    

Use them if you want to know what the size is for definite. C99 requires that these types have the sizes specified. All other types are platform dependent.

Other types are guaranteed to be a certain minimum width, as per limits.h. But they could equally be bigger than that. It is up to the implementation.

我乃一代侩神 2024-10-22 20:24:44

C99 的 stdint.h 实际上并不保证这些类型存在。引用维基百科

这些类型是可选的,除非实现支持宽度为 8、16、32 或 64 的类型,然后应将它们 typedef 为具有相应 N 的相应类型。

但是,如果它们确实存在,则它们将被正确的宽度。

C99's stdint.h does not actually guarantee that these types exist. To quote wikipedia:

These types are optional unless the implementation supports types with widths of 8, 16, 32 or 64, then it shall typedef them to the corresponding types with corresponding N.

However, if they do exist, they will be the correct widths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文