当我尝试sscanf(value, "%h" PRIu16 "B", &packet_size)
使用Clang 600.0.57(OS X)编译此代码时,我收到一条警告消息
.
warning: format specifies type 'unsigned char *' but the argument has type 'uint16_t *'
(aka 'unsigned short *') [-Wformat]
if (sscanf(value, "%h" PRIu16 "B", &packet_size) == 1) {
~~~~ ^~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
但是如果我删除修饰符"h",那么我在GCC 4.8.3(Scientific Linux 7)中得到以下错误.
warning: format ‘%u’ expects argument of type ‘unsigned int*’, but argument 3 has type ‘uint16_t* {aka short unsigned int*}’ [-Wformat=]
if (sscanf(value, "%" PRIu16 "B", &packet_size) == 1) {
^
Run Code Online (Sandbox Code Playgroud)
什么是sscanf中uint16_t*的正确和便携修饰符?
===在===下面添加了更多自我解释的例子
test.c的
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS 1
#endif
#include <inttypes.h>
#include <stdio.h>
int main() {
char* str = "16 bits";
uint16_t u16;
sscanf(str, "%h" PRIu16 " bits", &u16); // Clang warning
sscanf(str, "%" PRIu16 " bits", &u16); // GCC warning
sscanf(str, "%" SCNu16 " bits", &u16); // OK for both compilers
printf("%" PRIu16 " bits\n", u16);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
铿锵警告
$ clang test.c -Wall -Wextra
test.c:10:36: warning: format specifies type 'unsigned char *' but the argument
has type 'uint16_t *' (aka 'unsigned short *') [-Wformat]
sscanf(str, "%h" PRIu16 " bits", &u16); // Clang warning
~~~~ ^~~~
1 warning generated.
Run Code Online (Sandbox Code Playgroud)
GCC警告
$ gcc -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:11:3: warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 3 has type ‘uint16_t *’ [-Wformat=]
sscanf(str, "%" PRIu16 " bits", &u16); // GCC warning
^
Run Code Online (Sandbox Code Playgroud)
小智 7
作为@EOF说在他们的评论,fscanf并且fprintf都有这自己的宏.
在最终的C99草案中,§7.8.1第4节和第5节(第199页)说明<inttypes.h>应该定义以下宏:
fscanf有符号整数的宏是:SCNd N SCNdLEAST N SCNdFAST N SCNdMAX SCNdPTR
SCNi N SCNiLEAST N SCNiFAST N SCNiMAX SCNiPTR
fscanf无符号整数的宏是:SCNo N SCNoLEAST N SCNoFAST N SCNoMAX SCNoPTR
SCNu N SCNuLEAST N SCNuFAST N SCNuMAX SCNuPTR
SCNx N SCNxLEAST N SCNxFAST N SCNxMAX SCNxPTR
如果你想用uint16_t十进制数字读取fscanf,你必须使用SCNu16.
例:
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint16_t input;
int items = scanf("fetch %" SCNu16 " bits", &input);
if (items == 1) {
printf("I'm busy, go fetch those %" PRIu16 " bits yourself.\n", input);
} else {
printf("I don't understand what you're saying.\n")
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)