<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
Include the header, pick a width that matches your data, then use the matching limit macros.
Add #include <stdint.h> (C99 or later).
Choose exact (uint16_t), least (int_least32_t), or fast (int_fast8_t) types.
Bound checks with INT32_MAX, UINT8_MAX, and friends.
For portable printf, include <inttypes.h> and use PRId32 / PRIu64.
Piece
Required?
Role
#include <stdint.h>
Yes
Typedefs and limit macros
Width suffix (8/16/32/64)
Yes
How many bits you need
<inttypes.h>
No
Safe 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)
Reference
Cheat Sheet
Common exact-width types and limits on a typical two’s-complement desktop.
Topic
Detail
Header
#include <stdint.h> (C99+)
uint8_t
0 … 255 (UINT8_MAX)
int16_t
−32768 … 32767
int32_t
−231 … 231−1 (INT32_MAX)
uint32_t
0 … 4294967295
int_fast32_t
Fastest signed type with ≥ 32 bits
intptr_t
Signed integer that can hold a pointer
Print safely
#include <inttypes.h> + PRId32 / PRIu64
Try it
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.
Trace
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.
Code
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.
Include inttypes.h. It pulls in stdint.h and defines PRI* macros.
Split the format."%" PRId32 expands to the right specifier for int32_t.
Use PRIu64 for uint64_t. Avoids guessing between %llu and %lu.
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.
Analysis
Performance Notes
Approach
Cost
Best for
Exact types (int32_t)
Same as underlying type
Protocols, files, ABI fields
int_fastN_t
May be wider than N
Hot math when ≥ N bits is enough
Plain int
Natural register size
General-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.
Remember
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.