sscanf 中 uint16_t 的正确便携 (Clang/GCC) 修饰符是什么?
What is the correct and portable (Clang/GCC) modifier for uint16_t in sscanf?
我在尝试编译这段代码时收到一条警告消息
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) {
~~~~ ^~~~~~~~~~~~
但是如果我删除修饰符 "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) {
^
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;
}
叮当警告
$ 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.
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
^
正如@EOF 在他们的评论中所说,fscanf
和 fprintf
每个都有自己的宏。
在 final C99 draft 中,§7.8.1 第 4 条和第 5 条(第 199 页)指出 <inttypes.h>
应定义以下宏:
The fscanf
macros for signed integers are:
SCNdN SCNdLEASTN SCNdFASTN SCNdMAX SCNdPTR
SCNiN SCNiLEASTN SCNiFASTN SCNiMAX SCNiPTR
The fscanf
macros for unsigned integers are:
SCNoN SCNoLEASTN SCNoFASTN SCNoMAX SCNoPTR
SCNuN SCNuLEASTN SCNuFASTN SCNuMAX SCNuPTR
SCNxN SCNxLEASTN SCNxFASTN SCNxMAX SCNxPTR
如果要用 fscanf
将 uint16_t
读作十进制数,则必须使用 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;
}
我在尝试编译这段代码时收到一条警告消息
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) {
~~~~ ^~~~~~~~~~~~
但是如果我删除修饰符 "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) {
^
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;
}
叮当警告
$ 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.
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
^
正如@EOF 在他们的评论中所说,fscanf
和 fprintf
每个都有自己的宏。
在 final C99 draft 中,§7.8.1 第 4 条和第 5 条(第 199 页)指出 <inttypes.h>
应定义以下宏:
The
fscanf
macros for signed integers are:SCNdN SCNdLEASTN SCNdFASTN SCNdMAX SCNdPTR
SCNiN SCNiLEASTN SCNiFASTN SCNiMAX SCNiPTRThe
fscanf
macros for unsigned integers are:SCNoN SCNoLEASTN SCNoFASTN SCNoMAX SCNoPTR
SCNuN SCNuLEASTN SCNuFASTN SCNuMAX SCNuPTR
SCNxN SCNxLEASTN SCNxFASTN SCNxMAX SCNxPTR
如果要用 fscanf
将 uint16_t
读作十进制数,则必须使用 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;
}