stdint.h 和 inttypes.h 之间的区别
stdint.h 和 inttypes.h 有什么区别?
如果没有使用它们,则无法识别 uint64_t,但使用它们中的任何一个,它都是已定义的类型。
What is the difference between stdint.h and inttypes.h?
If none of them is used, uint64_t is not recognized but with either of them it is a defined type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
stdint.h
如果您想使用 C99 的指定宽度整数类型(即
int32_t
、uint16_t
等),包含此文件是“最低要求”。如果包含此文件,您将获得这些类型的定义,以便您能够在变量和函数的声明中使用这些类型并对这些数据类型进行操作。
inttypes.h
如果包含此文件,您将获得 stdint.h 提供的所有内容(因为 inttypes.h 包含 stdint.h),但您还将获得执行
的功能printf
和scanf
(以及fprintf
、fscanf
等)以可移植的方式使用这些类型。例如,您将获得PRIu64
宏,以便您可以printf
一个uint64_t
,如下所示:您想要使用
的一个原因例如,printf
与 inttypes.h 的区别是,uint64_t
在 Linux 中是long unsigned
,但在 Windows 中是long long unsigned
。因此,如果您只包含 stdint.h (而不是 inttypes.h),那么,要编写上述代码并使其在 Linux 和 Windows 之间保持交叉兼容,您将必须执行以下操作(注意丑陋的 #ifdef) :stdint.h
Including this file is the "minimum requirement" if you want to work with the specified-width integer types of C99 (i.e.
int32_t
,uint16_t
etc.).If you include this file, you will get the definitions of these types, so that you will be able to use these types in declarations of variables and functions and do operations with these datatypes.
inttypes.h
If you include this file, you will get everything that stdint.h provides (because inttypes.h includes stdint.h), but you will also get facilities for doing
printf
andscanf
(andfprintf
,fscanf
, and so on.) with these types in a portable way. For example, you will get thePRIu64
macro so that you canprintf
auint64_t
like this:One reason you would want to use
printf
with inttypes.h is, for example, thatuint64_t
islong unsigned
in Linux butlong long unsigned
in Windows. Thus, if you were to only include stdint.h (not inttypes.h), then, to write the above code and keep it cross-compatible between Linux and Windows, you would have to do the following (notice the ugly #ifdef):请参阅 wikipedia 文章以了解 inttypes.h。
使用 stdint.h 获取最少的定义集;如果您还需要 printf、scanf 等中的可移植支持,请使用 inttypes.h。
See the wikipedia article for inttypes.h.
Use stdint.h for a minimal set of definitions; use inttypes.h if you also need portable support for these in printf, scanf, et al.