h-stdint
Table of Contents
<stdint.h>
<stdint.h> provides fixed-width integer types (C99): int8_t, uint32_t, int64_t, etc., guaranteeing exact widths across platforms. Also includes intmax_t, intptr_t, and limit macros like INT32_MAX.
Use fixed-width types for binary I/O, hardware registers, and portable low-level code.
Example
This example queries the sizes of fixed-width integer types on the platform.
// compile: gcc -o stdintexample stdintexample.c // run: ./stdintexample // description: show fixed-width integer type sizes #include <stdint.h> #include <stdio.h> int main() { printf("int8_t = %zu bytes\n", sizeof(int8_t)); printf("int16_t = %zu bytes\n", sizeof(int16_t)); printf("int32_t = %zu bytes\n", sizeof(int32_t)); printf("int64_t = %zu bytes\n", sizeof(int64_t)); printf("intptr_t = %zu bytes\n", sizeof(intptr_t)); printf("INT32_MAX = %d\n", INT32_MAX); printf("UINT64_MAX = %llu\n", (unsigned long long)UINT64_MAX); return 0; }
Common types and macros
Fixed-width signed integers:
int8_t,int16_t,int32_t,int64_t: exactly N-bit signed integers- Guaranteed to exist and have exactly that size
Fixed-width unsigned integers:
uint8_t,uint16_t,uint32_t,uint64_t: exactly N-bit unsigned integers
Pointer-sized integers:
intptr_t,uintptr_t: guaranteed to hold any pointer safely- Use for pointer arithmetic without losing information
Widest types:
intmax_t,uintmax_t: widest signed and unsigned integers on platform- May be 64-bit or larger (e.g., 128-bit on some platforms)
Min/max macros (INT8_MIN through INT64_MIN, etc.):
INT8_MAX,INT8_MIN: ±128 for int8_tINT16_MAX,INT16_MIN: ±32,767 for int16_tINT32_MAX,INT32_MIN: ±2,147,483,647 for int32_tINT64_MAX,INT64_MIN: ±9,223,372,036,854,775,807 for int64_tUINT8_MAX,UINT16_MAX,UINT32_MAX,UINT64_MAX: unsigned limitsINTPTR_MAX,UINTPTR_MAX: pointer limitsINTMAX_MAX,UINTMAX_MAX: widest type limits
Common usage:
- Use fixed-width types (
uint32_t, etc.) for binary data, network protocols, hardware interfaces - Prefer
int,unsignedfor general integer arithmetic (these have efficient operations) - This header is more portable than
<limits.h>for specific-width guarantees
h-stdint.md · Last modified: by 127.0.0.1
