I get a warning when I try to compile this code
sscanf(value, "%h" PRIu16 "B", &packet_size)
with 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) {
~~~~ ^~~~~~~~~~~~
But if I remove the "h" modifier, I get the following error with 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) {
^
What is the correct and portable modifier for uint16_t * in sscanf?
=== Added a more self-evident example below ===
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);
sscanf(str, "%" PRIu16 " bits", &u16);
sscanf(str, "%" SCNu16 " bits", &u16);
printf("%" PRIu16 " bits\n", u16);
return 0;
}
Clan Warning
$ 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 Warning
$ 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);
^