inttypes.h
C also provides placeholders for printf()
and scanf()
in the header file inttypes.h for the four classes of integer types defined in stdint.h.
- Fixed-width integer types, such as int8_t.
- Minimum width integer types, e.g. int_least8_t.
- Fastest minimum width integer types, e.g. int_fast8_t.
- Maximum width integer type, e.g. intmax_t.
The placeholder for printf()
takes the form PRI + raw placeholder + type keyword/width
. For example, if the original placeholder is %d
, the corresponding placeholder would be as follows.
- PRIdn (fixed-width type)
- PRIdLEASTn (minimum width type)
- PRIdFASTn (fastest minimum width type)
- PRIdMAX (maximum width type)
The n
in the above placeholder can be substituted with 8, 16, 32 or 64.
Here is an example of usage.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main(void) {
int_least16_t x = 3490;
printf("The value is %" PRIdLEAST16 "! \n", x);
}
In the above example, PRIdLEAST16
corresponds to an integer of type int_least16_t with the original placeholder %d
. Also, the first argument to printf()
uses the automatic merging of multiple strings.
Here are the other placeholders corresponding to the original placeholder.
- %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 placeholder rules for scanf()
are similar.
- %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