C++ uint8_t、uint16_t、...的格式说明符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6993132/
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
Format specifiers for uint8_t, uint16_t, ...?
提问by Usman
If I have an integer variable I can use sscanf
as shown below by using the format specifier %d.
如果我有一个整数变量,我可以sscanf
通过使用格式说明符 %d来使用,如下所示。
sscanf (line, "Value of integer: %d\n", &my_integer);
Where can I find format specifiers for uint8_t
, uint16_t
, uint32_t
and uint64_t
?
我在哪里可以找到格式说明uint8_t
,uint16_t
,uint32_t
和uint64_t
?
uint64_t has probably %lu.
uint64_t 可能有 %lu。
回答by AProgrammer
They are declared in <inttypes.h>
as macros: SCNd8, SCNd16, SCNd32 and SCNd64.
Example (for int32_t):
它们被声明<inttypes.h>
为宏:SCNd8、SCNd16、SCNd32 和 SCNd64。示例(对于 int32_t):
sscanf (line, "Value of integer: %" SCNd32 "\n", &my_integer);
Their format is PRI (for printf)/SCN (for scan) then o, u, x, X d, i for the corresponding specifier then nothing, LEAST, FAST, MAX then the size (obviously there is no size for MAX). Some other examples: PRIo8, PRIuMAX, SCNoFAST16.
它们的格式是 PRI(对于 printf)/SCN(对于扫描),然后是 o, u, x, X d, i 对于相应的说明符然后什么都没有,LEAST、FAST、MAX 然后是大小(显然 MAX 没有大小)。其他一些示例:PRIo8、PRIuMAX、SCNoFAST16。
Edit: BTW a related questionasked why that method was used. You may find the answers interesting.
编辑:顺便说一句,一个相关的问题询问了为什么使用该方法。你可能会发现答案很有趣。
回答by Maxim Egorushkin
As others said, include <stdint.h>
header that defines the format macros. In C++, however, define __STDC_FORMAT_MACROS
prior to including it. From stdint.h:
正如其他人所说,包含<stdint.h>
定义格式宏的标头。然而,在 C++ 中,__STDC_FORMAT_MACROS
在包含它之前先定义。来自 stdint.h:
/* The ISO C99 standard specifies that these macros must only be
defined if explicitly requested. */
#if !defined __cplusplus || defined __STDC_FORMAT_MACROS
回答by Sebastian Mach
According to 7.19.6 Formatted input/output functionsof ISO/IEC 9899:TC2, there are no such format specifiers (so I doubt there any for C++2003). Even though there are some #define-macros available in C99's inttypes.h
, cinttypes
and inttypes.h
are not part of the current standard. Of course, fixed-size integer types are non-standard as well.
根据ISO/IEC 9899:TC2 的7.19.6 Formatted input/output functions,没有这样的格式说明符(所以我怀疑 C++2003 有没有)。尽管 C99 中有一些 #define-macros 可用,并且不是当前标准的一部分。当然,固定大小的整数类型也是非标准的。inttypes.h
cinttypes
inttypes.h
Anyways, I seriously recommemend using streams instead:
无论如何,我强烈建议改用流:
<any_type> x;
f >> x;
and be done. E.g.:
并完成。例如:
std::stringstream ss;
uint32_t u;
std::cin >> u;
This has the advantage that one time in the future, changing the type of the variable does not cause a cascade of subtle bugs and undefined behaviour.
这样做的好处是,将来更改变量的类型不会导致一连串微妙的错误和未定义的行为。
回答by Alejandro Caro
The right format to read a uint64_t
(typedef unsigned long long int) is, with scanf
and not sscanf
, "%" SCNu64
and for print is also SCNu64
Example. in your code you read for example my_integer variable, then you do
scanf ("Value of integer:%" SCNu64, & my_integer);
and to write the same but with printf.
读取uint64_t
(typedef unsigned long long int)的正确格式是 withscanf
和 not sscanf
,"%" SCNu64
对于打印也是SCNu64
示例。在您的代码中,您读取了例如 my_integer 变量,然后您可以
scanf ("Value of integer:%" SCNu64, & my_integer);
使用 printf 编写相同的内容。
回答by Jonathan Leffler
In C, the header is <inttypes.h>
, and formats such as SCNX8, SCNd16.
在 C 中,标头为<inttypes.h>
,以及 SCNX8、SCNd16 等格式。
The same header probably works for C++ too.
相同的头文件可能也适用于 C++。