C Standard Library
In C programming, when handling integer types, we often need to ensure code portability across different platforms. Different platforms may have different integer sizes and representations, which can cause code to behave inconsistently in different environments.
To solve the above problems, the C standard library provides theheader file, which defines a set of fixed-size integer types and corresponding formatting macros, helping developers write portable code.
inttypes.h is a very important header file in the C standard library. It provides a set of fixed-width integer types and corresponding formatting macros. By using these types and macros, developers can ensure that integer sizes and formatted output are consistent across different platforms, thereby avoiding potential problems.
Fixed-width integer types
inttypes.h defines a set of fixed-width integer types that have the same size on different platforms. The following are common fixed-width integer types:
int8_t: 8-bit signed integeruint8_t: 8-bit unsigned integerint16_t: 16-bit signed integeruint16_t: 16-bit unsigned integerint32_t: 32-bit signed integeruint32_t: 32-bit unsigned integerint64_t: 64-bit signed integeruint64_t: 64-bit unsigned integer
These types ensure that integer sizes are consistent across different platforms, thereby improving code portability.
Formatting macros
inttypes.h also defines a set of formatting macros for formatting fixed-width integer types in functions such asprintfandscanfprintf. These macros ensure that the formatted output of integers is consistent across different platforms.
The following are common formatting macros:
PRId8: used to formatint8_tsigned integers of typePRIu8: used to formatuint8_tunsigned integers of typePRId16: used to formatint16_tsigned integers of typePRIu16: used to formatuint16_tunsigned integers of typePRId32: used to formatint32_tsigned integers of typePRIu32: used to formatuint32_tunsigned integers of typePRId64: used to formatint64_tsigned integers of typePRIu64: used to formatuint64_tunsigned integers of type
These macros are used as follows:
Example
#include
#include
int
main
(
)
{
int32_t
myInt
=
42
;
printf
(
"The value of myInt is: %"
PRId32
"\n"
,
myInt
)
;
return
0
;
}
In the code above,PRId32 The macro is used to format int32_t signed integers of type, ensuring consistent output across different platforms.
The output is:
The value of myInt is: 42
Other macros
inttypes.h also defines some other useful macros, for example:
INT8_MIN,INT8_MAX:int8_tThe minimum and maximum values of a typeUINT8_MAX:uint8_tThe maximum value of a typeINT16_MIN,INT16_MAX:int16_tThe minimum and maximum values of a typeUINT16_MAX:uint16_tThe maximum value of a typeINT32_MIN,INT32_MAX:int32_tThe minimum and maximum values of a typeUINT32_MAX:uint32_tThe maximum value of a typeINT64_MIN,INT64_MAX:int64_tThe minimum and maximum values of a typeUINT64_MAX:uint64_tThe maximum value of a type
These macros help developers safely use fixed-width integer types in code, avoiding overflow and other potential problems.
Example
The following is an example of usinginttypes.hcomplete example showing how to define and use fixed-width integer types and their formatting macros:
Example
#include
#include
int
main
(
)
{
int32_t
myInt
=
42
;
uint64_t
myUInt
=
1234567890123456789ULL
;
printf
(
"The value of myInt is: %"
PRId32
"\n"
,
myInt
)
;
printf
(
"The value of myUInt is: %"
PRIu64
"\n"
,
myUInt
)
;
printf
(
"The minimum value of int32_t is: %"
PRId32
"\n"
,
INT32_MIN
)
;
printf
(
"The maximum value of uint64_t is: %"
PRIu64
"\n"
,
UINT64_MAX
)
;
return
0
;
}
The output is:
The value of myInt is: 42
The value of myUInt is: 1234567890123456789
The minimum value of int32_t is: -2147483648
The maximum value of uint64_t is: 18446744073709551615
In the above code, we defined a int32_t signed integer of type and an uint64_t unsigned integer of type, and printed them using the corresponding formatting macros. We also used INT32_MIN and UINT64_MAX macros to print the minimum and maximum values of these types.