uint8_t,uint16_t,...的格式说明符?

cho*_*hoc 54 c++ scanf c++11

如果我有一个整数变量,我可以使用sscanf格式说明符%d,如下所示.

sscanf (line, "Value of integer: %d\n", &my_integer);
Run Code Online (Sandbox Code Playgroud)

我在哪里可以找到格式说明uint8_t,uint16_t,uint32_tuint64_t

uint64_t可能有%lu.

APr*_*mer 84

它们<inttypes.h>以宏的形式声明:SCNd8,SCNd16,SCNd32和SCNd64.示例(对于int32_t):

sscanf (line, "Value of integer: %" SCNd32 "\n", &my_integer);
Run Code Online (Sandbox Code Playgroud)

它们的格式是PRI(对于printf)/ SCN(对于扫描)然后o,u,x,X d,i对应的说明符然后没有,LEAST,FAST,MAX然后是大小(显然没有MAX的大小).其他一些例子:PRIo8,PRIuMAX,SCNoFAST16.

编辑:BTW一个相关的问题,问为什么使用该方法.您可能会发现答案很有趣.

  • 它应该是`uint32_t`的`SCNu32`.`SCNi32`用于`int32_t`. (3认同)

Max*_*kin 7

正如其他人所说,include <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
Run Code Online (Sandbox Code Playgroud)

  • 根据C++ 0X,"<cinttypes>定义的宏是无条件提供的.特别是,C标准脚注182中提到的符号__STDC_FORMAT_MACROS在C++中没有任何作用." 但定义__STDC_FORMAT_MACROS可能有助于扩展未更新以遵循C++ 0X. (4认同)