C语言 stdint.h 和 inttypes.h 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7597025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:45:24  来源:igfitidea点击:

difference between stdint.h and inttypes.h

cuint64stdint

提问by mahmood

What is the difference between stdint.h and inttypes.h?

stdint.h 和 inttypes.h 有什么区别?

If none of them is used, uint64_t is not recognized but with either of them it is a defined type.

如果没有使用它们,则无法识别 uint64_t,但对于它们中的任何一个,它都是已定义的类型。

采纳答案by Ed Staub

See the wikipedia article for inttypes.h.

有关 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.

将 stdint.h 用于最少的定义集;如果您还需要在 printf、scanf 等中对这些进行便携式支持,请使用 inttypes.h。

回答by Mikko ?stlund

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.

如果您想使用 C99 的指定宽度整数类型(即“int32_t”、“uint16_t”等),则包含此文件是“最低要求”。如果包含此文件,您将获得这些类型的定义,以便您能够在变量和函数的声明中使用这些类型并使用这些数据类型进行操作。

inttypes.h

类型.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 and scanf(and "fprintf, "fscanf", and so on.) with these types in a portable way. For example, you will get the "PRIu16" macro so that you can printf an uint16_t integer like this:

如果包含此文件,您将获得 stdint.h 提供的所有内容(因为 inttypes.h 包含 stdint.h),但您还将获得执行 printf 和 scanf(以及“fprintf”、“fscanf”等)的工具。 ) 以可移植的方式使用这些类型。例如,您将获得“PRIu16”宏,以便您可以像这样打印一个 uint16_t 整数:

#include <stdio.h>
#include <inttypes.h>
int main (int argc, char *argv[]) {

    // Only requires stdint.h to compile:
    uint16_t myvar = 65535;

    // Requires inttypes.h to compile:
    printf("myvar=%" PRIu16 "\n", myvar);  
}