Espacios de nombres
Variantes
Acciones

Códigos de formato de printf/scanf

De cppreference.com
< cpp‎ | io‎ | c


Las familias de funciones ? printf y ? scanf de entrada/salida en C++ soportan una cadena de texto conteniendo códigos y banderas de formato para indicar diferentes tipos y opciones de formato y justificación.


Código Formato
%c un char (caracter)
%d un entero con signo en notación de base decimal
%i un entero con signo
%e reales((pseudoreales como double)) en notación científica indicando el exponente con "e"
%E reales((pseudoreales como double)) en notación científica indicando el exponente con "E"
%f formato de punto flotante
%g la opción más corta entre "%e" y "%f"
%G la opción más corta entre "%E" y "%F"
%o un entero sin signo en notación de base octal
%s una cadena de caracteres
%u un entero sin signo
%x un entero sin signo en notación de base hexadecimal, usando minúsculas para los dígitos extendidos
%X un entero sin signo en notación de base hexadecimal, usando mayúsculas para los dígitos extendidos
%p un puntero
%n un puntero a un entero en el cual se deposita la cantidad de caracteres escritos hasta el momento

Contenido

[editar] Modificadores de Formato

Algunos de los códigos superiores pueden aceptar caracteres modificadores de formato para ajustar aún mejor la entrada o salida de datos de maneras específicas:

[editar] Ancho Mínimo

An integer placed between a % sign and the format command acts as a minimum field width specifier, and pads the output with spaces or zeros to make it long enough. If you want to pad with zeros, place a zero before the minimum field width specifier:

     %012d

You may also specify the minimum field width in an int variable if instead of a number you put the * sign:

     int width = 12;
     int age = 100;
     printf("%*d", width, age);

[editar] Precisión

You can also include a precision modifier, in the form of a .N where N is some number, before the format command:

     %012.4d

The precision modifier has different meanings depending on the format command being used:

  • With %e, %E, and %f, the precision modifier lets you specify the number of decimal places desired. For example, %12.6f will display a floating number at least 12 digits wide, with six decimal places.
  • With %g and %G, the precision modifier determines the maximum number of significant digits displayed.
  • With %s, the precision modifier simply acts as a maximum field length, to complement the minimum field length that precedes the period.

As with field width specifier, you may use an int variable to specify the precision modifier by using the * sign:

     const char* msg = "Hello printf";
     int string_size = strlen (msg);
     printf("msg: %.*s", string_size, msg);

[editar] Justificación

All of printf's output is right-justified, unless you place a minus sign right after the % sign. For example,

     %-12.4f

will display a floating point number with a minimum of 12 characters, 4 decimal places, and left justified.

[editar] Tipos de Datos asociados

You may modify the %d, %i, %o, %u, and %x type specifiers with the letter l and the letter h to specify long and short data types (e.g. %hd means a short integer).

The %e, %f, and %g type specifiers can have the letter l before them to indicate that a double follows. The %g, %f, and %e type specifiers can be preceded with the character # to ensure that the decimal point will be present, even if there are no decimal digits.

[editar] Base de numeración

The use of the # character with the %x type specifier indicates that the hexidecimal number should be printed with the 0x prefix.

The use of the # character with the %o type specifier indicates that the octal value should be displayed with a 0 prefix.

[editar] Signo

Inserting a plus sign + into the type specifier will force positive values to be preceded by a + sign. Putting a space character ' ' there will force positive values to be preceded by a single space character.

  1. ---


You can also include constant escape sequences in the output string.