C Standard Library
is a header file introduced by the C99 standard. It provides definitions of integer types with explicit sizes and signedness, ensuring consistency across different platforms.
The library is designed to compensate for the inconsistency of integer type sizes across different platforms in C, thereby improving code portability and maintainability.
It is mainly 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-specific integer sizes.
By providing fixed-width integer types and related macros, programmers can explicitly specify the size of the integers they need, without relying on platform implementation details.
The main purposes are:
-
Provides fixed-width integer types (such as
int8_t,int16_t, etc.), ensuring their sizes are consistent across different platforms. -
Defines platform-independent integer types (such as
int_least8_t,int_fast16_t, etc.), used to optimize performance and memory usage. -
Provides widest integer types (such as
intmax_t,uintmax_t), used to represent the largest possible integers.
Fixed-width integer types
These types explicitly specify their width (number of bits), ensuring the same size on 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 is not defined.
Minimum-width integer types
These types have at least the specified width, but may be larger.
| Type | Description |
|---|---|
int_least8_t |
At least 8-bit signed integer |
int_least16_t |
At least 16-bit signed integer |
int_least32_t |
At least 32-bit signed integer |
int_least64_t |
At least 64-bit signed integer |
uint_least8_t |
At least 8-bit unsigned integer |
uint_least16_t |
At least 16-bit unsigned integer |
uint_least32_t |
At least 32-bit unsigned integer |
uint_least64_t |
At least 64-bit unsigned integer |
Fastest minimum-width integer types
These are the fastest integer types with at least the specified width, typically used for performance optimization.
| Type | Description |
|---|---|
int_fast8_t |
Fastest signed integer with 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 with 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 |
Widest integer types
These types are used to represent the largest possible integers.
| Type | Description |
|---|---|
intmax_t |
Widest signed integer |
uintmax_t |
Widest unsigned integer |
Pointer-width integer types
These types are used to represent integers of pointer size.
| Type | Description |
|---|---|
intptr_t |
Signed integer capable of storing a pointer |
uintptr_t |
Unsigned integer capable of storing a pointer |
Macro definition
Some macros are also defined to represent the maximum and minimum values of specific types.
| macro | Description |
|---|---|
INT8_MIN |
int8_t Minimum value of |
INT8_MAX |
int8_t Maximum value of |
UINT8_MAX |
uint8_t Maximum value of |
INT16_MIN |
int16_t Minimum value of |
INT16_MAX |
int16_t Maximum value of |
UINT16_MAX |
uint16_t Maximum value of |
INT32_MIN |
int32_t Minimum value of |
INT32_MAX |
int32_t Maximum value of |
UINT32_MAX |
uint32_t Maximum value of |
INT64_MIN |
int64_t Minimum value of |
INT64_MAX |
int64_t Maximum value of |
UINT64_MAX |
uint64_t Maximum value of |
INTMAX_MIN |
intmax_t Minimum value of |
INTMAX_MAX |
intmax_t Maximum value of |
UINTMAX_MAX |
uintmax_t Maximum value of |
Example
The following is an example of using example:
Example
#include
#include
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
;
// Widest integer types
intmax_t
e
=
INTMAX_MAX
;
// Output values
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
-
Only available in C99 and later versions. -
Fixed-width types (such as
int32_t) may not be available on some platforms, so you should check whether they are defined before using them. -
Use
It can improve code portability, especially in scenarios where precise control over integer sizes is required.