CKTIS / C
<inttypes.h> — formatos portáteis de inteiros
O cabeçalho <inttypes.h> inclui <stdint.h> e disponibiliza macros PRI e SCN adequadas aos tipos inteiros. Permitem imprimir e ler inteiros sem ter de adivinhar o modificador de comprimento apropriado.
Versão: C23; repository compiler flag, production runtime not attested
Sintaxe
#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> disponibiliza macros de literais de cadeia: PRI para a família printf e SCN para a família scanf, incluindo variantes para caracteres largos. Acrescenta % separadamente; os literais adjacentes são concatenados, como em "%" PRIdMAX. Não coloques o nome da macro entre aspas.
Todas as macros indicadas em syntax seguem a mesma regra. Os sufixos 8, 16, 32, 64 selecionam tipos intN_t/uintN_t de largura exata; LEASTN seleciona int_leastN_t/uint_leastN_t, e FASTN seleciona int_fastN_t/uint_fastN_t. Estes últimos são geralmente os mais rápidos entre os tipos com pelo menos N bits de largura, mas a norma não garante que sejam os mais rápidos para todos os fins. MAX corresponde a intmax_t/uintmax_t; PTR corresponde a intptr_t/uintptr_t, não a ponteiros.
PRId e PRIi imprimem valores decimais com sinal; PRIo, PRIu, PRIx e PRIX imprimem valores sem sinal em octal, decimal, hexadecimal com letras minúsculas e hexadecimal com letras maiúsculas. Em C23, PRIb (%b) e PRIB (%B) imprimem valores sem sinal em binário. SCNd lê um decimal com sinal; SCNi deteta a base. SCNo, SCNu e SCNx leem valores sem sinal em octal, decimal e hexadecimal, respetivamente.
Os tipos de largura exata e os tipos PTR são opcionais. PRI exige o tipo correspondente. Para as famílias PRI indicadas em syntax, C23 exige que a macro seja definida quando o tipo correspondente existe, à exceção das macros PRIB, que são opcionais mesmo quando o tipo correspondente existe. SCN exige também um modificador de comprimento de entrada adequado. Não troques PRI e SCN: a leitura exige um ponteiro para o tipo correspondente. Uma incompatibilidade de tipos ou um resultado lido não representável causa comportamento indefinido; verifica o número de atribuições. Para texto não fiável, prefere strtoimax/strtoumax com verificações do intervalo e do fim da conversão.
Identificadores de referência abrangidos por esta entrada: 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.
Exemplos
#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;
}
Resultado
decimal=-42 hex=2A
O código apresentado e o resultado declarado foram verificados num ambiente isolado pelo método registado na prova de verificação desta versão exata.