不同体系结构上 C 中 int 的大小
我知道 C 语言的规范并没有规定每个整数类型(例如 int
)的确切大小。
我想知道的是:C(而不是C++)中是否有一种方法可以定义具有特定大小的整数类型,以确保它在不同的体系结构中是相同的?比如:
typedef int8 <an integer with 8 bits>
typedef int16 <an integer with 16 bits>
或者任何其他方式允许程序的其他部分在不同的体系结构上编译。
I am aware that the specification of the C language does not dictate the exact size of each integer type (e.g., int
).
What I am wondering is: Is there a way in C (not C++) to define an integer type with a specific size that ensures it will be the same across different architectures? Like:
typedef int8 <an integer with 8 bits>
typedef int16 <an integer with 16 bits>
Or any other way that will allow other parts of the program to be compiled on different architecture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您想要的是
,符合 C 标准(“C99”)的编译器将实现它。不幸的是,这不包括微软。幸运的是,一个开源项目为 Windows 提供了
,请参阅 msinttypes。这将允许您使用
int32_t
和uint32_t
,以及 8、16 和 64 以及许多其他数字。注意:头文件本身在标准中不是可选的,但是头文件中的大多数类型都是单独可选的。有些则不然。最常用的类型是可选类型,但没有什么可以阻止您使用必需的类型。问题是,如果实现提供了标头,那么实际上它们定义了所有类型。
What you want is
<stdint.h>
, which compilers that conform to the C standard ("C99") will implement. Unfortunately, this does not include Microsoft. Fortunately, an open-source project provides a<stdint.h>
for Windows, see msinttypes.This will allow you to use
int32_t
anduint32_t
, plus 8, 16, and 64, and many others.Note: the header file itself is not optional in the standard, however, most of the types in the header are individually optional. Some are not. The most commonly used types are the optional ones, but nothing stops you from using the required ones. The thing is, if an implementation provides the header at all, in practice they define all of the types.
C99,在 stdint.h 中,定义了类似
int8_t
的类型和int16_t。C99, in stdint.h, defines types like
int8_t
andint16_t
.不,C 标准指定了整型的最小尺寸,但不保证最大尺寸。
如果该大小的类型可用,则实现应提供 intN_t 类型。我只提到,由于您有一个跨平台标签 - 没有正确位宽类型的实现不需要提供这些类型。
通常,您可以选择(通过设置定义,例如
cc -D_INT16_IS_INT
和#ifdef
s)用于特定位大小的正确类型。您可以使用CHAR_BIT
和sizeof()
为 C 代码支持的每个平台计算出所需的定义。c1x 草案 (n1362) 的相关部分是:
7.18.1.1 精确宽度整数类型
typedef 名称
intN_t
指定宽度为N
,无填充位,以及二进制补码表示。因此,int8_t
表示宽度恰好为 8 位的有符号整数类型。typedef 名称
uintN_t
指定宽度为N
的无符号整数类型。因此,uint24_t
表示宽度恰好为 24 位的无符号整数类型。这些类型是可选的。但是,如果实现提供宽度为 8、16、32 或 64 位的整数类型,无填充位,并且(对于有符号类型)具有二进制补码表示形式,则它应定义相应的 typedef 名称。
关于类型的选择,类似这样的事情就足够了:
No, the C standard specifies minimum sizes for integral types but makes no guarantee on maximum sizes.
An implementation shall provide
intN_t
types if types of that size are available. I only mention that since you had a cross-platform tag - an implementation that does not have a type of the correct bit width does not need to provide those types.You can generally select (with setting defines with, for example,
cc -D_INT16_IS_INT
and#ifdef
s) the correct type to use for a specific bit size. You can work out the required defines for each platform you want to support with C code usingCHAR_BIT
andsizeof()
.The relevant section of the c1x draft (n1362) is:
7.18.1.1 Exact-width integer types
The typedef name
intN_t
designates a signed integer type with widthN
, no padding bits, and a two’s complement representation. Thus,int8_t
denotes a signed integer type with a width of exactly 8 bits.The typedef name
uintN_t
designates an unsigned integer type with widthN
. Thus,uint24_t
denotes an unsigned integer type with a width of exactly 24 bits.These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.
Regarding the selection of types, something like this should suffice:
除非您使用
#ifdef
左右检查每个平台,否则我怀疑这很容易实现。但许多图书馆已经为您完成了这项任务。对于 MSVC,它是__int8
、__int16, &c.
GTK 库有类似的 typedef。
Unless you check for each platform with
#ifdef
or so, I doubt it's easily possible. But many libraries already do that task for you. For MSVC it's__int8
,__int16
, &c. The GTK library has similar typedefs.您可能想查看 pstdint.h。它是 stdint.h 的可移植实现,并且不需要 C99 编译器支持。
You might want to take a look at pstdint.h. It is a portable implementation of stdint.h, and does not require C99 compiler support.
据我所知,答案是否定的。我们为不同的平台编写代码,并且只使用 #if/#else 为特定平台使用 typedef。例如在 Win32 上: typedef int int32;
As far as I know, the answer is no. We code for different platforms and we just use typedefs for the specific platforms using #if/#else. For example on Win32: typedef int int32;
您始终可以编写一个使用无符号字符向量作为数字的算术库。这样您就可以使用您想要的任何位长度的数字,甚至允许位长度变化。
实际上,您不需要实现这样的库,因为 GNU MP 已经处理了这个问题。
http://gmplib.org/
You could always write an arithmetic library that used vectors of unsigned char for the numbers. That way you could use numbers of whatever bit length you want, and even allow the bit length to vary.
Actually, you don't need to implement such a library because GNU MP handles this already.
http://gmplib.org/