Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C

<inttypes.h> — formatos portátiles de enteros

El encabezado <inttypes.h> incluye <stdint.h> y proporciona macros PRI y SCN adecuadas para los tipos enteros. Permiten imprimir y leer enteros sin tener que adivinar el modificador de longitud apropiado.

Versión: C23; repository compiler flag, production runtime not attested

Sintaxis

#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> proporciona macros de literales de cadena: PRI para la familia printf y SCN para la familia scanf, también en variantes para caracteres anchos. Añade % por separado; los literales adyacentes se concatenan, como en "%" PRIdMAX. No escribas el nombre de la macro entre comillas.

Todas las macros enumeradas en syntax siguen la misma regla. Los sufijos 8, 16, 32, 64 seleccionan tipos intN_t/uintN_t de ancho exacto; LEASTN selecciona int_leastN_t/uint_leastN_t, y FASTN selecciona int_fastN_t/uint_fastN_t. Estos últimos suelen ser los más rápidos entre los tipos de al menos N bits de ancho, pero el estándar no garantiza que lo sean para todos los usos. MAX corresponde a intmax_t/uintmax_t; PTR corresponde a intptr_t/uintptr_t, no a punteros.

PRId y PRIi imprimen valores decimales con signo; PRIo, PRIu, PRIx y PRIX imprimen valores sin signo en octal, decimal, hexadecimal con letras minúsculas y hexadecimal con letras mayúsculas, respectivamente. En C23, PRIb (%b) y PRIB (%B) imprimen valores sin signo en binario. SCNd lee un decimal con signo; SCNi detecta la base. SCNo, SCNu y SCNx leen valores sin signo en octal, decimal y hexadecimal, respectivamente.

Los tipos de ancho exacto y los tipos PTR son opcionales. PRI requiere el tipo correspondiente. Para las familias PRI enumeradas en syntax, C23 exige que se defina la macro si existe el tipo correspondiente, salvo las macros PRIB, que son opcionales incluso si existe dicho tipo. SCN también requiere un modificador de longitud de entrada adecuado. No intercambies PRI y SCN: la lectura requiere un puntero al tipo correspondiente. Una incompatibilidad de tipos o un resultado leído que no se pueda representar causa comportamiento indefinido; comprueba el número de asignaciones. Para texto no confiable, prefiere strtoimax/strtoumax con comprobaciones del rango y del final de la conversión.

Identificadores de referencia incluidos en 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.

Ejemplos

#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

El código mostrado y el resultado declarado se verificaron en un entorno aislado mediante el método registrado en la evidencia de verificación de esta versión exacta.

Fuentes