<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.
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; }
Fixed-width signed integers:
int8_t, int16_t, int32_t, int64_t: exactly N-bit signed integersFixed-width unsigned integers:
uint8_t, uint16_t, uint32_t, uint64_t: exactly N-bit unsigned integersPointer-sized integers:
intptr_t, uintptr_t: guaranteed to hold any pointer safelyWidest types:
intmax_t, uintmax_t: widest signed and unsigned integers on platformMin/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 limitsCommon usage:
uint32_t, etc.) for binary data, network protocols, hardware interfacesint, unsigned for general integer arithmetic (these have efficient operations)<limits.h> for specific-width guarantees