I am using library <inttypes.h>
(<stdint.h>
) for compatibility across platforms in usigned types. When compiling on my MacOSX with -Wall
options no warnings arise, while on Ubuntu 20.04 I get
mvmpi.c: In function ‘main’:
mvmpi.c:33:9: warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 6 has type ‘uint16_t *’ {aka ‘short unsigned int *’} [-Wformat=]
33 | "%"PRIu32"%"PRIu32"%lf%"PRIu16, &N, &M, &L, &SN) != EOF) {
| ^~~ ~~~
| |
| uint16_t * {aka short unsigned int *}
In file included from stdbasic.h:33,
from mvmpilib.h:8,
from mvmpi.c:4:
/usr/include/inttypes.h:103:19: note: format string is defined here
103 | # define PRIu16 "u"
The formatting complains about variable SN
which is declared to be uint16_t
variable and then it uses "%"PRIu16
format specifier, but surprisingly it appears that PRIu16
is defined in C sources libraries as unsigned int
, while uint16_t
would be (regularly) short unsigned int
.
What is going on here? How to solve and mantain cross platform compatibility? Of course uint16_t
from documentation is defined with at least 16 bits, but if the format specifier is more, then also it must be bigger in order to maintain coherence.
PRIu16
is for printing uint16_t
.
You have to use SCNu16
for reading uint16_t
.
Also you have to use SCNu32
, not PRIu32
, for reading uint32_t
.