Reading this SO question, I started wondering - what is the correct way to use scanf
/printf
(and family) with fixed size types?
For example
The correct way is to use inttypes.h which defines standard macros for printf
family and the scanf
family, e.g.
printf ("%" PRId16, short_int);
scanf ("%" SCNd16, &short_int);
The inttypes.h header file contains macros that define the correct format specifiers for fixed-width integers defined in stdint.h. For example, the type specifier for printf()
and int16_t
is the macro named PRId16
. Example:
int16_t x;
scanf("%" SCNd16, &x);
printf("You have entered: %" PRId16 "\n", x);