C stdint.h Header

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is stdint.h?

<stdint.h> is a C99 header that defines fixed-width integer types such as int32_t and uint8_t, plus matching limit macros like INT32_MAX. Use it when exact bit sizes matter.

Remember
#include <stdint.h>

Exact width:   int8_t  int16_t  int32_t  int64_t
               uint8_t uint16_t uint32_t uint64_t

Least width:   int_least32_t   (at least 32 bits)
Fast width:    int_fast16_t    (fastest ≥ 16 bits)

Limits:        INT32_MAX  UINT8_MAX  INT64_MIN
Pointers:      intptr_t / uintptr_t

int size varies — int32_t does not
UsePrefer
Exact bit fields / protocolsuint8_t, int32_t, …
Everyday countersint (or size_t for sizes)
Built-in type rangeslimits.h (INT_MAX)

How to Use stdint.h in C

Include the header, pick a width that matches your data, then use the matching limit macros.

  1. Add #include <stdint.h> (C99 or later).
  2. Choose exact (uint16_t), least (int_least32_t), or fast (int_fast8_t) types.
  3. Bound checks with INT32_MAX, UINT8_MAX, and friends.
  4. For portable printf, include <inttypes.h> and use PRId32 / PRIu64.
PieceRequired?Role
#include <stdint.h>YesTypedefs and limit macros
Width suffix (8/16/32/64)YesHow many bits you need
<inttypes.h>NoSafe printf/scanf format macros

Pseudocode

Pseudocode
include stdint.h

// protocol / file field — exact width
u8  = uint8_t byte
u32 = uint32_t length

if value > UINT8_MAX:
    reject  // does not fit in one byte

// everyday loop — plain int is fine
for i in 0 .. n-1:
    ...

// print int32_t portably
include inttypes.h
printf("%" PRId32, x)

Cheat Sheet

Common exact-width types and limits on a typical two’s-complement desktop.

TopicDetail
Header#include <stdint.h> (C99+)
uint8_t0 … 255 (UINT8_MAX)
int16_t−32768 … 32767
int32_t−231 … 231−1 (INT32_MAX)
uint32_t0 … 4294967295
int_fast32_tFastest signed type with ≥ 32 bits
intptr_tSigned integer that can hold a pointer
Print safely#include <inttypes.h> + PRId32 / PRIu64

Live Preview

Explore typical ranges for common stdint.h types (browser simulation of usual two’s-complement sizes).

Simulates typical fixed-width ranges (same ideas as the C examples below).

Result
int32_t — exactly 32 bits (signed)

INT32_MIN = -2147483648
INT32_MAX =  2147483647

Use for portable 32-bit fields.

Worked Walkthrough — One Packet Byte

A protocol field must fit in one byte. Trace a valid value and a value that overflows uint8_t.

Calculation
UINT8_MAX = 255

Call 1: store(200)
  200 <= 255 → OK
  write uint8_t = 200 ✓

Call 2: store(300)
  300 <= 255 → false
  reject — would wrap to 44 if cast blindly ✓

Why uint8_t, not unsigned char?
  Same size on most systems, but the name
  documents "exactly 8 bits" for the format.

C Examples

Compile with gcc file.c -std=c11 -o out. Sample output matches a typical desktop (8/16/32/64-bit types available).

Example 1 — Fixed-Width Sizes

Confirm that exact-width types have the bit sizes you expect.

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

int main(void) {
    printf("uint8_t:  %zu bits\n", sizeof(uint8_t) * 8);
    printf("int16_t:  %zu bits\n", sizeof(int16_t) * 8);
    printf("int32_t:  %zu bits\n", sizeof(int32_t) * 8);
    printf("uint64_t: %zu bits\n", sizeof(uint64_t) * 8);
    printf("int:      %zu bits\n", sizeof(int) * 8);

    return 0;
}

How It Works

  1. Include stdint.h. Brings in the fixed-width typedefs.
  2. Measure with sizeof. Multiply by 8 for bits (assuming 8-bit bytes).
  3. Exact widths hold. uint8_t is 8 bits; int32_t is 32.
  4. Compare to int. Plain int is often 32 bits here—but that is not guaranteed everywhere.

Example 2 — Guard with Limit Macros

Reject values that cannot fit in a uint8_t field.

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

int store_byte(int value, uint8_t *out) {
    if (value < 0 || value > UINT8_MAX) {
        return -1;
    }
    *out = (uint8_t)value;
    return 0;
}

int main(void) {
    uint8_t b;

    if (store_byte(200, &b) == 0) {
        printf("stored: %u\n", (unsigned)b);
    }
    if (store_byte(300, &b) != 0) {
        printf("300: rejected (UINT8_MAX = %u)\n",
               (unsigned)UINT8_MAX);
    }

    return 0;
}

How It Works

  1. Read UINT8_MAX. The macro is 255 for an 8-bit unsigned type.
  2. Validate first. Values outside 0…255 return an error instead of wrapping.
  3. Store safely. 200 fits → cast to uint8_t and keep it.
  4. Reject overflow. 300 would become 44 if cast blindly—so we refuse it.

Example 3 — Portable printf with inttypes.h

stdint.h defines the types; inttypes.h adds format macros so prints stay portable.

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

int main(void) {
    int32_t  a = -42;
    uint64_t b = 10000000000ULL;

    printf("int32_t:  %" PRId32 "\n", a);
    printf("uint64_t: %" PRIu64 "\n", b);
    printf("INT32_MAX: %" PRId32 "\n", INT32_MAX);

    return 0;
}

How It Works

  1. Include inttypes.h. It pulls in stdint.h and defines PRI* macros.
  2. Split the format. "%" PRId32 expands to the right specifier for int32_t.
  3. Use PRIu64 for uint64_t. Avoids guessing between %llu and %lu.
  4. Limits print the same way. INT32_MAX is an int32_t-compatible constant.

Edge Cases & Pitfalls

Check these before relying on fixed-width types in portable code.

printf

Do not assume %d works for int32_t

On some platforms int32_t is not int. Prefer PRId32 from inttypes.h.

exact

Exact-width types may be missing

If a platform has no 64-bit integer, int64_t is absent. Fall back to int_least64_t or intmax_t.

wrap

Narrowing casts wrap silently

Casting 300 to uint8_t becomes 44. Check against UINT8_MAX first.

int

Not every int should be int32_t

Loop indices and return codes are fine as int. Reserve fixed widths for formats and hardware.

Performance Notes

ApproachCostBest for
Exact types (int32_t)Same as underlying typeProtocols, files, ABI fields
int_fastN_tMay be wider than NHot math when ≥ N bits is enough
Plain intNatural register sizeGeneral-purpose counters

Using stdint.h has no runtime tax—the typedefs compile to ordinary integers. Pick width for correctness first; use int_fast* only when profiling says it helps.

Key Takeaways

  • Role: portable typedefs for exact (and least/fast) integer widths.
  • Limits: use INT32_MAX, UINT8_MAX, and friends—not magic numbers.
  • Print: inttypes.h + PRI* macros for portable output.
  • Choice: fixed width for formats; plain int for everyday logic.

One line: stdint.h answers “I need exactly N bits everywhere”—so protocols and files stay portable.

Frequently Asked Questions

stdint.h (C99) typedefs integers with explicit bit widths—int8_t, uint16_t, int32_t, uint64_t—plus least-width and fast-width types. Use it when exact sizes matter for file formats, network packets, or hardware registers.
Use int32_t when the value must be exactly 32 bits on every platform. Use int for everyday counters where the platform's natural size is fine. int size varies; int32_t does not.
int_leastN_t is the smallest signed type with at least N bits. int_fastN_t is the fastest signed type with at least N bits. Exact-width types (int16_t) are exactly N bits when available.
Include inttypes.h and use PRI macros: printf("%" PRId32, value) for int32_t and printf("%" PRIu64, value) for uint64_t. Plain %d or %lu can mismatch on some platforms.
Not guaranteed. The standard requires int64_t only if the implementation provides a 64-bit integer. Most desktops do; some tiny embedded targets may not. Check INT64_MAX or use intmax_t for the widest type.
limits.h documents ranges of built-in types (INT_MAX for int). stdint.h defines typedefs with chosen widths and matching macros like INT32_MAX. Use both when writing portable low-level code.

Next: stdio.h

Print and read data with printf, scanf, and file streams.

stdio.h tutorial →

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