C Reference ยท Types and Literals
C fixed-width integers: int32_t, uint64_t and friends
Reference for the C fixed-width integer types from stdint.h: intN_t, uintN_t, least and fast variants, limit macros, literal macros and inttypes.h printing.
The types int, long and short have sizes that change from one platform to another. When a program must be sure of how many bits a value has, as in file formats, network protocols, hardware registers and hashes, it uses the fixed-width integer types from <stdint.h>: int8_t, int16_t, int32_t, int64_t and their unsigned partners uint8_t to uint64_t.
The exact-width types are optional in theory (a machine without an 8-bit byte cannot provide int8_t) but present on every platform you are likely to meet. They are typedefs for the built-in types, so int32_t is usually just another name for int.
#include <stdint.h> uint8_t flags = 0xA5; int32_t count = -2000000000; uint64_t id = 1ULL << 40;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <stdint.h>
| Family | Examples | Meaning |
|---|---|---|
| Exact width | int8_t, int16_t, int32_t, int64_t, uint8_t ... uint64_t |
Exactly N bits, two's complement for signed, no padding. Optional. |
| Least width | int_least8_t ... uint_least64_t |
Smallest type with at least N bits. Required. |
| Fastest | int_fast8_t ... uint_fast64_t |
Fastest type with at least N bits. Required. |
| Largest | intmax_t, uintmax_t |
The widest integer types. |
| Pointer-sized | intptr_t, uintptr_t |
See intptr_t. |
Limit macros: INT8_MIN, INT8_MAX, UINT8_MAX, INT16_MIN, ... UINT64_MAX, SIZE_MAX, INTMAX_MAX. Literal macros: INT32_C(5), UINT64_C(1), INTMAX_C(...) give a constant of the right type. C23 adds width macros such as INT32_WIDTH and UINT64_WIDTH.
Printing: <inttypes.h> defines format macros: PRId32, PRIu64, PRIx8, and for scanf, SCNd32, SCNu64. They are string pieces glued between literals: "%" PRIu64 "\n".
Errors / undefined behavior: signed fixed-width arithmetic overflows like int (undefined). Arithmetic on int8_t and int16_t is promoted to int, so sums do not overflow until stored back.
Usage notes
- Choose by meaning. Use fixed-width types for data layout and protocols; use
intfor loop counters and small values; usesize_tfor sizes and indices. - Do not assume a struct matches a file format just because its members are fixed-width: padding and byte order still apply. See Struct padding and Endianness.
uint8_tis usuallyunsigned char, so it is allowed to alias any object, but this is not guaranteed by the standard for the typedef alone.int_fasttypes can be surprising, for example 64 bits on glibc x86-64 forint_fast16_t, which makes arrays of them larger than expected.- Match
printfspecifiers.%dwith anint64_tis undefined behavior on platforms whereint64_tislong. UsePRId64. - Mixed signedness follows the usual conversion rules; see unsigned types.
- Added in C99 (
<stdint.h>,<inttypes.h>). C++ spells themstd::int32_tin<cstdint>.
Related
stdint.h,inttypes.h,int,shortandlong,size_tandptrdiff_t,_BitInt- Lessons: Integers, Limits