C Fixed Width Integers

C Tutorial

C Fixed Width Integers

C Fixed Width Integers were introduced in C99 to ensure variables always have a predictable size (in bits), regardless of system or compiler differences.

For example, on one system an int may be 2 bytes (16-bit) while on another it may be 4 bytes (32-bit).
Fixed-width types solve this problem.

These types are defined in:

#include <stdint.h>

 Exact-Width Integer Types

These guarantee that the variable is exactly the specified number of bits.

TypeSigned VersionUnsigned VersionSize
8-bitint8_tuint8_t1 byte
16-bitint16_tuint16_t2 bytes
32-bitint32_tuint32_t4 bytes
64-bitint64_tuint64_t8 bytes

Example:


 


 Minimum and Fastest Integer Types

 Minimum-width types

Guaranteed to be at least the specified width.

SignedUnsigned
int_least8_tuint_least8_t
int_least16_tuint_least16_t
int_least32_tuint_least32_t
int_least64_tuint_least64_t

 Fastest integer types

Optimized for speed (may be larger than required).

SignedUnsigned
int_fast8_tuint_fast8_t
int_fast16_tuint_fast16_t
int_fast32_tuint_fast32_t
int_fast64_tuint_fast64_t

 Special Type: intptr_t and uintptr_t

These are integer types that guaranteed to hold a pointer address.

intptr_t ptrValue;
uintptr_t uptrValue;

Useful in embedded systems and low-level programming.


 Format Specifiers (from inttypes.h)

For portable printing, you should include:

#include <inttypes.h>

Examples:


 Example: Using All Fixed Integer Types


 


🧠 Why Use Fixed-Width Integers?

BenefitReason
PortabilitySame size across platforms
Predictable memory usageImportant in embedded systems
Binary communicationNetworking, file formats require exact size
PerformanceOptimized fixed-width data for microcontrollers

Summary

GroupExample TypesPurpose
Exact-widthint8_t, uint32_tSame size on any system
Minimum-widthint_least16_tAt least specified size
Fastest-widthint_fast32_tBest performance
Pointer-compatibleintptr_tStore pointer as integer

You may also like...