C Standard Library<stdint.h>
<stdint.h>Is a header file introduced by the C99 standard. It provides definitions for several integer types that have explicit size and signedness attributes, ensuring consistency across different platforms.
<stdint.h>The library is intended to address the issue of inconsistent integer type sizes across different platforms in C, thereby improving code portability and maintainability.
<stdint.h>Primarily used to define fixed-size integer types, the minimum/maximum values of integer types, and type qualifiers for integer types. This avoids relying on compiler- or OS platform-specific integer sizes.
<stdint.h>By providing fixed-width integer types and related macros, programmers can explicitly specify the required integer size without relying on platform implementation details.
<stdint.h>The main purpose is:
Provide fixed-width integer types (such as
int8_t、int16_tetc.), ensuring that their sizes are consistent across different platforms.Define platform-independent integer types (such as
int_least8_t、int_fast16_tetc.), used to optimize performance and memory usage.Provide maximum-width integer types (such as
intmax_t、uintmax_t), used to represent the largest possible integer.
Fixed-width integer types
These types explicitly specify their width (number of bits), ensuring that they have the same size across different platforms.
| Type | Description |
|---|---|
int8_t |
8-bit signed integer |
int16_t |
16-bit signed integer |
int32_t |
32-bit signed integer |
int64_t |
64-bit signed integer |
uint8_t |
8-bit unsigned integer |
uint16_t |
16-bit unsigned integer |
uint32_t |
32-bit unsigned integer |
uint64_t |
64-bit unsigned integer |
Note:
-
If a platform does not support a particular fixed-width type, that type will not be defined.
Minimum-width integer types
These types have at least the specified width, but may be larger.
| Type | Description |
|---|---|
int_least8_t |
Signed integer of at least 8 bits |
int_least16_t |
Signed integer of at least 16 bits |
int_least32_t |
Signed integer of at least 32 bits |
int_least64_t |
Signed integer of at least 64 bits |
uint_least8_t |
Unsigned integer of at least 8 bits |
uint_least16_t |
Unsigned integer of at least 16 bits |
uint_least32_t |
Unsigned integer of at least 32 bits |
uint_least64_t |
Unsigned integer of at least 64 bits |
Fastest minimum-width integer types
These types are the fastest integer types with the specified width, typically used for performance optimization.
| Type | Description |
|---|---|
int_fast8_t |
Fastest signed integer of at least 8 bits |
int_fast16_t |
Fastest signed integer with at least 16 bits |
int_fast32_t |
Fastest signed integer with at least 32 bits |
int_fast64_t |
Fastest signed integer with at least 64 bits |
uint_fast8_t |
Fastest unsigned integer of at least 8 bits |
uint_fast16_t |
Fastest unsigned integer with at least 16 bits |
uint_fast32_t |
Fastest unsigned integer with at least 32 bits |
uint_fast64_t |
Fastest unsigned integer with at least 64 bits |
Maximum-width integer types
These types are used to represent the largest possible integer values.
| Type | Description |
|---|---|
intmax_t |
Signed integer of maximum width |
uintmax_t |
Unsigned integer of maximum width |
Pointer-width integer types
These types are used to represent integers of pointer size.
| Type | Description |
|---|---|
intptr_t |
Signed integer capable of holding a pointer |
uintptr_t |
Unsigned integer capable of holding a pointer |
Macro definition
<stdint.h>It also defines macros for representing the maximum and minimum values of specific types.
| Macro | Description |
|---|---|
INT8_MIN |
int8_tminimum value of |
INT8_MAX |
int8_tmaximum value of |
UINT8_MAX |
uint8_tmaximum value of |
INT16_MIN |
int16_tminimum value of |
INT16_MAX |
int16_tmaximum value of |
UINT16_MAX |
uint16_tmaximum value of |
INT32_MIN |
int32_tminimum value of |
INT32_MAX |
int32_tmaximum value of |
UINT32_MAX |
uint32_tmaximum value of |
INT64_MIN |
int64_tminimum value of |
INT64_MAX |
int64_tmaximum value of |
UINT64_MAX |
uint64_tmaximum value of |
INTMAX_MIN |
intmax_tminimum value of |
INTMAX_MAX |
intmax_tmaximum value of |
UINTMAX_MAX |
uintmax_tmaximum value of |
Example
The following is a use of<stdint.h>example:
Example
#include <stdint.h>
int main() {
// Fixed-width integer types
int32_t a = 100;
uint64_t b = 1000000000000ULL;
// Minimum-width integer types
int_least16_t c = 200;
// Fastest minimum-width integer types
int_fast32_t d = 300;
// Maximum-width integer types
intmax_t e = INTMAX_MAX;
// Output value
printf("a = %d\n", a);
printf("b = %llu\n", b);
printf("c = %d\n", c);
printf("d = %d\n", d);
printf("e = %jd\n", e);
return 0;
}
Notes
<stdint.h>Only available in C99 and later versions.Fixed-width types (such as
int32_t) may not be available on some platforms; therefore, you should check whether it is defined before use.Usage
<stdint.h>It can improve code portability, especially in scenarios that require precise control over integer sizes.