GCC中是否有固定大小的整数? [英] Are there fixed size integers in GCC?

查看:111
本文介绍了GCC中是否有固定大小的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSVC ++编译器上,可以使用 __ int8 __ int16 __ int32 和具有特定大小的整数的类似类型。这对于需要处理低级数据结构(如自定义文件格式,硬件控制数据结构等)的应用程序非常有用。

是否有类似的等效I可以在GCC编译器上使用吗?

解决方案

ISO标准C从C99标准开始,添加标准头< stdint.h> 定义了这些:

  uint8_t  - 无符号8位
int8_t - 带符号8位
uint16_t - 无符号16位
int16_t - 带符号16位
uint32_t - 无符号32位
int32_t - 带符号32位
uint64_t - 无符号64位
int64_t - 有符号64位

我一直使用这些类型。 p>

这些类型仅在实现支持具有适当大小和特征的预定义类型时才会定义(大多数情况下都是这样)。

< stdint.h> 还定义了名称为for m (u)int_leastN_t (具有至少指定宽度的类型)和(u)int_fastN_t (具有至少指定宽度的最快类型);这些类型是必需的。



如果您使用的旧版本实现不支持< stdint.h> ,你可以推出自己的;一个实现是 Doug Gwyn的q8


On the MSVC++ compiler, one can use the __int8, __int16, __int32 and similar types for integers with specific sizes. This is extremely useful for applications which need to work with low-level data structures like custom file formats, hardware control data structures and the like.

Is there a similar equivalent I can use on the GCC compiler?

解决方案

ISO standard C, starting with the C99 standard, adds the standard header <stdint.h> that defines these:

uint8_t  - unsigned 8 bit
int8_t   - signed 8 bit
uint16_t - unsigned 16 bit
int16_t  - signed 16 bit
uint32_t - unsigned 32 bit
int32_t  - signed 32 bit
uint64_t - unsigned 64 bit
int64_t  - signed 64 bit

I use these types all the time.

These types are defined only if the implementation supports predefined types with the appropriate sizes and characteristics (which most do).

<stdint.h> also defines types with names of the form (u)int_leastN_t (types that have at least the specified width) and (u)int_fastN_t (the "fastest" types that have at least the specified width); these types are mandatory.

If you're using an old implementation that doesn't support <stdint.h>, you can roll your own; one implementation is Doug Gwyn's "q8".

这篇关于GCC中是否有固定大小的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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