C inttypes.h Header

Beginner
6 min read
Updated: Nov 2026
3 examples
C99

What Is inttypes.h?

<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.

Remember
#include <inttypes.h>

int32_t  a;  printf("%" PRId32 "\n", a);
uint64_t b;  printf("%" PRIu64 "\n", b);
uint8_t  c;  printf("%02" PRIx8 "\n", c);

sscanf(s, "%" SCNd32, &a);

intmax_t / uintmax_t -> PRIdMAX / PRIuMAX
UsePrefer
Print int32_t / int64_tPRId32 / PRId64
Print unsigned or hexPRIu* / PRIx* / PRIX*
Read fixed-width valuesSCNd* / SCNu* with sscanf
Parse text into intmax_tstrtoimax()

How to Use inttypes.h in C

Pick a macro family, then splice it between string literals in the format.

  1. Add #include <inttypes.h>; it already includes stdint.h.
  2. Use PRI macros for printf and SCN macros for scanf.
  3. Choose the conversion letter: d signed, u unsigned, x/X hex, o octal.
  4. Add the width: 8, 16, 32, 64, or MAX.
  5. Write the format as adjacent literals: "%" PRId32 "\n".
PieceRequired?Role
#include <inttypes.h>YesDeclares the macros and functions
PRI* / SCN* macrosYesString literals for the length modifier and letter
stdint.hIncludedProvides int32_t, uint64_t, and friends

Pseudocode

Pseudocode
printf("a = %" PRId32 "\n", a);
  -> preprocessor: printf("a = %d\n", a);

PRId64 may be "ld" or "lld" depending on platform:
printf("b = %" PRId64 "\n", b);
  -> printf("b = %ld\n", b);   or   "%lld"

Cheat Sheet

Replace N with 8, 16, 32, or 64.

NameMeaning
PRIdNprintf signed decimal
PRIuNprintf unsigned decimal
PRIxN, PRIXNprintf lowercase / uppercase hexadecimal
PRIdMAX, PRIuMAXprintf for intmax_t / uintmax_t
PRIdPTR, PRIuPTRprintf for intptr_t / uintptr_t
SCNdN, SCNuNscanf signed / unsigned
imaxabs, imaxdivAbsolute value and division for intmax_t
strtoimax, strtoumaxParse strings into intmax_t / uintmax_t

Worked Walkthrough — Format Macro Expansion

See what the preprocessor does with a PRI macro.

Calculation
int32_t a = -42;

code:     printf("a = %" PRId32 "\n", a);
step 1:   PRId32 expands to "d"
step 2:   adjacent literals join: "a = %" "d" "\n" -> "a = %d\n"
output:   a = -42

uint8_t flags = 0xAB;
printf("0x%02" PRIX8 "\n", flags);   -> 0xAB

C Examples

Compile with gcc file.c -std=c11 -o out.

Example 1 — Print Fixed-Width Integers

Print a signed 32-bit value, an unsigned 64-bit value, and a byte in hexadecimal.

C
#include <inttypes.h>
#include <stdio.h>

int main(void) {
    int32_t a = -42;
    uint64_t b = 1234567890123ULL;
    uint8_t flags = 0xAB;

    printf("a = %" PRId32 "\n", a);
    printf("b = %" PRIu64 "\n", b);
    printf("flags = 0x%02" PRIX8 "\n", flags);
    return 0;
}

How It Works

  1. Match macro to type. PRId32 goes with int32_t, PRIu64 with uint64_t.
  2. Join the literals. The quotes end before the macro and restart after it.
  3. Hex with width. %02 pads to two digits and PRIX8 prints uppercase hex.

Example 2 — Read Fixed-Width Integers with sscanf

Parse two numbers from a string using SCN macros.

C
#include <inttypes.h>
#include <stdio.h>

int main(void) {
    int32_t x;
    uint64_t y;

    if (sscanf("1234 5678", "%" SCNd32 " %" SCNu64, &x, &y) == 2) {
        printf("x = %" PRId32 ", y = %" PRIu64 "\n", x, y);
    }
    return 0;
}

How It Works

  1. Use SCN, not PRI. The scanf family needs the SCN macros.
  2. Pass addresses. &x and &y receive the parsed values.
  3. Check the count. sscanf returns how many items it converted; 2 means both succeeded.

Example 3 — Parse with strtoimax and Take the Absolute Value

Convert a long number from text and see where parsing stopped.

C
#include <inttypes.h>
#include <stdio.h>

int main(void) {
    char *end;
    intmax_t v = strtoimax("-9000000000xyz", &end, 10);

    printf("value = %" PRIdMAX "\n", v);
    printf("abs   = %" PRIdMAX "\n", imaxabs(v));
    printf("rest  = %s\n", end);
    return 0;
}

How It Works

  1. Wide result. intmax_t holds at least 64 bits, so 9 billion fits easily.
  2. Stop at junk. strtoimax stops at x and sets end to that position.
  3. 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.

Performance & Portability Notes

ApproachCostBest for
PRI / SCN macrosResolved at compile time; no run-time costPortable fixed-width I/O
strtoimaxSame work as strtoll on most platformsParsing with error detection via endptr
(long long)x with %lldAlso portable but wider than neededQuick 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.

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.

Next: iso646.h

Use and, or, not, bitand, and xor as readable operator spellings.

iso646.h tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful