整型在不同的架构C尺寸

整型在不同的架构C尺寸

问题描述:

据我所知,C语言的规范没有规定每个整数类型的确切大小(例如, INT )。

I am aware that the specification of the C language does not dictate the exact size of each integer type (e.g., int).

我想知道的是:是否有C(不是C ++)的方式来定义与保证这将是跨不同体系结构相同的特定大小的整数类型?这样的:

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.

您需要的是&LT; stdint.h&GT; ,它的编译器符合C标准( C99)将实施。不幸的是,这不包括微软。幸运的是,一个开源项目提供了一个&LT; stdint.h&GT; 适用于Windows,看到的 msinttypes 。

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.

这将允许您使用 int32_t uint32_t的,加8,16,64,和许多其他

This will allow you to use int32_t and uint32_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.