The C99 specified inttypes.h header includes macros for providing format specifiers for the fixed-width integer types provided by stdint.h (beginning with PRI). While stdint.h is on the 'freestanding' list of headers and thus is always provided by the implementation, inttypes.h is not.
This means that when compiling for an unhosted environment (say bare-metal), you must provide inttypes.h yourself if you want to provide a standards-compliant printf that can handle fixed-width types.
However, I cannot figure out how to correctly determine at compile-time what each macro should be without manually inspecting the implementation's stdint.h, a task that would need to be repeated not just for each target platform, but for every supported compiler.
I tried an implementation via C11's _Generic but discovered one of the limitations of that feature.
Is there a standard way to do this?
It is not possible to do that. In essence,
inttypes.h
exists to give you portability, you can't "guess" it. It has to be given by the compiler. On top of that, theoretically, there just might not exist aprintf
format specifier to print the types fromstdint
.Yes - contact your compiler provider and make a feature request. While waiting for it, read your compiler documentation and/or source or contact upstream for more information and create your own
myinttypes.h
with the information that you need.That's simple - cast the types to standard types before printing.
64_t
tolong long
,32_t
tolong
,16_t
to anint
and8_t
tochar
and print them using standard%lld
...%hhd
format specifiers.That there is no guarantee that
inttypes.h
is provided, that is not a guarantee that it is not provided - it might be. The question sounds more like a theoretical question - I do not think any common compiler in the wild comes withoutinttypes.h
.