inttypes.h in C Language
In C, the inttypes.h
header file provides format specifiers for integer types defined in stdint.h
that can be used with printf()
and scanf()
. Here are the four types of integer types available:
- Fixed-width integer types, e.g.,
int8_t
- Minimum-width integer types, e.g.,
int_least8_t
- Fastest minimum-width integer types, e.g.,
int_fast8_t
- Maximum-width integer types, e.g.,
intmax_t
The format specifiers for printf()
are formed by combining PRI
+ original specifier + type keyword/width. For example, if the original specifier is %d
, the format specifiers are as follows:
PRIdn
(fixed-width types)PRIdLEASTn
(minimum-width types)PRIdFASTn
(fastest minimum-width types)PRIdMAX
(maximum-width types)
The n
in these specifiers can be replaced with 8
, 16
, 32
, or 64
, depending on the integer width.
Here is an example of how to use these specifiers:
1 |
|
In this example, PRIdLEAST16
is used to match the type int_least16_t
, with the original specifier %d
. Note that the first argument in printf()
uses concatenated strings.
Here are the format specifiers for other original placeholders:
%i
:PRIin
,PRIiLEASTn
,PRIiFASTn
,PRIiMAX
%o
:PRIon
,PRIoLEASTn
,PRIoFASTn
,PRIoMAX
%u
:PRIun
,PRIuLEASTn
,PRIuFASTn
,PRIuMAX
%x
:PRIxn
,PRIxLEASTn
,PRIxFASTn
,PRIxMAX
%X
:PRIXn
,PRIXLEASTn
,PRIXFASTn
,PRIXMAX
The scanf()
format specifiers follow a similar pattern:
%d
:SCNdn
,SCNdLEASTn
,SCNdFASTn
,SCNdMAX
%i
:SCNin
,SCNiLEASTn
,SCNiFASTn
,SCNiMAX
%o
:SCNon
,SCNoLEASTn
,SCNoFASTn
,SCNoMAX
%u
:SCNun
,SCNuLEASTn
,SCNuFASTn
,SCNuMAX
%x
:SCNxn
,SCNxLEASTn
,SCNxFASTn
,SCNxMAX
These conventions ensure that the correct specifier is used for each integer type, making the code more portable and readable.