<inttypes.h> extends stdint.h with format macros for printf and scanf (PRId32, PRIu64, SCNd16) and a few helpers such as strtoimax and imaxabs.
Fixed-width types such as int32_t and uint64_t have no single portable length modifier. The macros expand to the right string literal for each platform, and you splice them into the format string.
Wide result.intmax_t holds at least 64 bits, so 9 billion fits easily.
Stop at junk.strtoimax stops at x and sets end to that position.
Absolute value.imaxabs returns an intmax_t, printed with PRIdMAX.
Edge Cases & Pitfalls
Most inttypes.h mistakes are format and type mismatches.
type
Match macro and type
Using PRId64 with an int32_t argument is undefined. Pair each macro with its exact type.
quote
Keep macros outside the quotes
Write "%" PRId32. The macro is a string literal and must sit between quoted pieces, not inside them.
scan
PRI is not SCN
scanf needs SCNd32 and SCNu64. Using the PRI form there is a mistake.
c++
Old C++ compilers
Before C++11 you had to define __STDC_FORMAT_MACROS before including the header to get the macros.
Analysis
Performance & Portability Notes
Approach
Cost
Best for
PRI / SCN macros
Resolved at compile time; no run-time cost
Portable fixed-width I/O
strtoimax
Same work as strtoll on most platforms
Parsing with error detection via endptr
(long long)x with %lld
Also portable but wider than needed
Quick one-off prints
The macros are plain string literals, so they cost nothing at run time. Prefer them over guessing %d or %ld for fixed-width types.
Remember
Key Takeaways
Role: portable printf/scanf formats for int32_t, uint64_t, and friends.
Pattern:"%" PRId32—the macro sits between quoted literals.
Scanning: use SCN macros with scanf, not PRI.
Extras:strtoimax and imaxabs work with intmax_t.
One line:inttypes.h turns stdint.h types into something you can safely print and parse on every platform.
Frequently Asked Questions
inttypes.h is a C99 header that adds printf/scanf format macros (PRId32, PRIu64, SCNd32) and helper functions like strtoimax and imaxabs for the fixed-width types in stdint.h.
int32_t may be int on one platform and long on another. PRId32 expands to the correct conversion for the current platform.
PRId32 is for printf-style output. SCNd32 is for scanf-style input. They can expand to different strings.
No. inttypes.h includes stdint.h, so one include gives you both the types and the format macros.
Use PRIdMAX for signed and PRIuMAX for unsigned values of intmax_t and uintmax_t.
imaxabs, imaxdiv, strtoimax, strtoumax, wcstoimax, and wcstoumax.