CKTIS / C
<inttypes.h> — portable integer formats
The <inttypes.h> header includes <stdint.h> and supplies PRI and SCN macros matched to integer types. They let you print and scan integers without guessing the appropriate length modifier.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <inttypes.h>
/* Usage (with <stdio.h>):
Call sscanf only if any decimal value it can match in text is known
to be representable as intmax_t. An out-of-range conversion has
undefined behavior even if you check the return value.
intmax_t value;
if (sscanf(text, "%" SCNdMAX, &value) == 1) {
printf("%" PRIdMAX "\n", value);
}
*/
/* Format macro inventory (availability depends on the implementation):
PRId8 PRIdLEAST8 PRIdFAST8 PRId16 PRIdLEAST16 PRIdFAST16 PRId32 PRIdLEAST32 PRIdFAST32 PRId64 PRIdLEAST64 PRIdFAST64 PRIdMAX PRIdPTR
PRIi8 PRIiLEAST8 PRIiFAST8 PRIi16 PRIiLEAST16 PRIiFAST16 PRIi32 PRIiLEAST32 PRIiFAST32 PRIi64 PRIiLEAST64 PRIiFAST64 PRIiMAX PRIiPTR
PRIo8 PRIoLEAST8 PRIoFAST8 PRIo16 PRIoLEAST16 PRIoFAST16 PRIo32 PRIoLEAST32 PRIoFAST32 PRIo64 PRIoLEAST64 PRIoFAST64 PRIoMAX PRIoPTR
PRIu8 PRIuLEAST8 PRIuFAST8 PRIu16 PRIuLEAST16 PRIuFAST16 PRIu32 PRIuLEAST32 PRIuFAST32 PRIu64 PRIuLEAST64 PRIuFAST64 PRIuMAX PRIuPTR
PRIx8 PRIxLEAST8 PRIxFAST8 PRIx16 PRIxLEAST16 PRIxFAST16 PRIx32 PRIxLEAST32 PRIxFAST32 PRIx64 PRIxLEAST64 PRIxFAST64 PRIxMAX PRIxPTR
PRIX8 PRIXLEAST8 PRIXFAST8 PRIX16 PRIXLEAST16 PRIXFAST16 PRIX32 PRIXLEAST32 PRIXFAST32 PRIX64 PRIXLEAST64 PRIXFAST64 PRIXMAX PRIXPTR
C23 unsigned binary output (%b):
PRIb8 PRIbLEAST8 PRIbFAST8 PRIb16 PRIbLEAST16 PRIbFAST16 PRIb32 PRIbLEAST32 PRIbFAST32 PRIb64 PRIbLEAST64 PRIbFAST64 PRIbMAX PRIbPTR
C23 optional unsigned binary output macros (%B):
PRIB8 PRIBLEAST8 PRIBFAST8 PRIB16 PRIBLEAST16 PRIBFAST16 PRIB32 PRIBLEAST32 PRIBFAST32 PRIB64 PRIBLEAST64 PRIBFAST64 PRIBMAX PRIBPTR
Scanning macros:
SCNd8 SCNdLEAST8 SCNdFAST8 SCNd16 SCNdLEAST16 SCNdFAST16 SCNd32 SCNdLEAST32 SCNdFAST32 SCNd64 SCNdLEAST64 SCNdFAST64 SCNdMAX SCNdPTR
SCNi8 SCNiLEAST8 SCNiFAST8 SCNi16 SCNiLEAST16 SCNiFAST16 SCNi32 SCNiLEAST32 SCNiFAST32 SCNi64 SCNiLEAST64 SCNiFAST64 SCNiMAX SCNiPTR
SCNo8 SCNoLEAST8 SCNoFAST8 SCNo16 SCNoLEAST16 SCNoFAST16 SCNo32 SCNoLEAST32 SCNoFAST32 SCNo64 SCNoLEAST64 SCNoFAST64 SCNoMAX SCNoPTR
SCNu8 SCNuLEAST8 SCNuFAST8 SCNu16 SCNuLEAST16 SCNuFAST16 SCNu32 SCNuLEAST32 SCNuFAST32 SCNu64 SCNuLEAST64 SCNuFAST64 SCNuMAX SCNuPTR
SCNx8 SCNxLEAST8 SCNxFAST8 SCNx16 SCNxLEAST16 SCNxFAST16 SCNx32 SCNxLEAST32 SCNxFAST32 SCNx64 SCNxLEAST64 SCNxFAST64 SCNxMAX SCNxPTR
*/<inttypes.h> supplies string-literal macros: PRI for the printf family and SCN for the scanf family, including wide-character variants. Add % separately; adjacent literals concatenate, as in "%" PRIdMAX. Do not put the macro name inside quotes.
Every macro listed in syntax follows the same rule. Suffixes 8, 16, 32, 64 select exact-width intN_t/uintN_t; LEASTN selects int_leastN_t/uint_leastN_t, and FASTN selects int_fastN_t/uint_fastN_t. These are usually the fastest types among those with a width of at least N bits, but the standard does not guarantee that they are fastest for every purpose. MAX selects intmax_t/uintmax_t; PTR selects intptr_t/uintptr_t, not pointers.
PRId and PRIi print signed decimal; PRIo, PRIu, PRIx, PRIX print unsigned octal, decimal, lowercase hexadecimal, and uppercase hexadecimal. In C23, PRIb (%b) and PRIB (%B) print unsigned values in binary. SCNd reads signed decimal; SCNi detects the base. SCNo, SCNu, SCNx read unsigned octal, decimal, and hexadecimal respectively.
Exact-width and PTR types are optional. PRI requires the corresponding type. For the PRI families listed in syntax, C23 requires the macro when the corresponding type exists, except for PRIB macros, which are optional even when the corresponding type exists. SCN additionally requires a suitable input length modifier. Do not interchange PRI and SCN: scanning requires a pointer to the matching type. A type mismatch or unrepresentable scanned result causes undefined behavior; check assignment counts. For untrusted text, prefer strtoimax/strtoumax with range and end-of-conversion checks.
Reference identifiers covered by this entry: PRId8, PRIdLEAST8, PRIdFAST8, PRId16, PRIdLEAST16, PRIdFAST16, PRId32, PRIdLEAST32, PRIdFAST32, PRId64, PRIdLEAST64, PRIdFAST64, PRIdPTR, PRIi8, PRIiLEAST8, PRIiFAST8, PRIi16, PRIiLEAST16, PRIiFAST16, PRIi32, PRIiLEAST32, PRIiFAST32, PRIi64, PRIiLEAST64, PRIiFAST64, PRIiMAX, PRIiPTR, PRIo8, PRIoLEAST8, PRIoFAST8, PRIo16, PRIoLEAST16, PRIoFAST16, PRIo32, PRIoLEAST32, PRIoFAST32, PRIo64, PRIoLEAST64, PRIoFAST64, PRIoMAX, PRIoPTR, PRIu8, PRIuLEAST8, PRIuFAST8, PRIu16, PRIuLEAST16, PRIuFAST16, PRIu32, PRIuLEAST32, PRIuFAST32, PRIu64, PRIuLEAST64, PRIuFAST64, PRIuMAX, PRIuPTR, PRIx8, PRIxLEAST8, PRIxFAST8, PRIx16, PRIxLEAST16, PRIxFAST16, PRIx32, PRIxLEAST32, PRIxFAST32, PRIx64, PRIxLEAST64, PRIxFAST64, PRIxMAX, PRIxPTR, PRIX8, PRIXLEAST8, PRIXFAST8, PRIX16, PRIXLEAST16, PRIXFAST16, PRIX32, PRIXLEAST32, PRIXFAST32, PRIX64, PRIXLEAST64, PRIXFAST64, PRIXMAX, PRIXPTR, PRIb8, PRIbLEAST8, PRIbFAST8, PRIb16, PRIbLEAST16, PRIbFAST16, PRIb32, PRIbLEAST32, PRIbFAST32, PRIb64, PRIbLEAST64, PRIbFAST64, PRIbMAX, PRIbPTR, PRIB8, PRIBLEAST8, PRIBFAST8, PRIB16, PRIBLEAST16, PRIBFAST16, PRIB32, PRIBLEAST32, PRIBFAST32, PRIB64, PRIBLEAST64, PRIBFAST64, PRIBMAX, PRIBPTR, SCNd8, SCNdLEAST8, SCNdFAST8, SCNd16, SCNdLEAST16, SCNdFAST16, SCNd32, SCNdLEAST32, SCNdFAST32, SCNd64, SCNdLEAST64, SCNdFAST64, SCNdMAX, SCNdPTR, SCNi8, SCNiLEAST8, SCNiFAST8, SCNi16, SCNiLEAST16, SCNiFAST16, SCNi32, SCNiLEAST32, SCNiFAST32, SCNi64, SCNiLEAST64, SCNiFAST64, SCNiMAX, SCNiPTR, SCNo8, SCNoLEAST8, SCNoFAST8, SCNo16, SCNoLEAST16, SCNoFAST16, SCNo32, SCNoLEAST32, SCNoFAST32, SCNo64, SCNoLEAST64, SCNoFAST64, SCNoMAX, SCNoPTR, SCNu8, SCNuLEAST8, SCNuFAST8, SCNu16, SCNuLEAST16, SCNuFAST16, SCNu32, SCNuLEAST32, SCNuFAST32, SCNu64, SCNuLEAST64, SCNuFAST64, SCNuMAX, SCNuPTR, SCNx8, SCNxLEAST8, SCNxFAST8, SCNx16, SCNxLEAST16, SCNxFAST16, SCNx32, SCNxLEAST32, SCNxFAST32, SCNx64, SCNxLEAST64, SCNxFAST64, SCNxMAX, SCNxPTR.
Examples
#include <inttypes.h>
#include <stdio.h>
int main(void)
{
intmax_t value = 0;
if (sscanf("-42", "%" SCNdMAX, &value) != 1) {
return 1;
}
if (printf("decimal=%" PRIdMAX " hex=%" PRIXMAX "\n",
value, (uintmax_t)42) < 0) {
return 1;
}
return 0;
}
Result
decimal=-42 hex=2A
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.