如果我有一个整数变量,我可以使用sscanf格式说明符%d,如下所示.
sscanf (line, "Value of integer: %d\n", &my_integer);
Run Code Online (Sandbox Code Playgroud)
我在哪里可以找到格式说明uint8_t,uint16_t,uint32_t和uint64_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一个相关的问题,问为什么使用该方法.您可能会发现答案很有趣.
正如其他人所说,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)