1.printf Type Field Characters
The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI compatible.
Character | Type | Output format |
---|---|---|
c | int or wint_t | When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character. |
C | int or wint_t | When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character. |
d | int | Signed decimal integer. |
i | int | Signed decimal integer. |
o | int | Unsigned octal integer. |
u | int | Unsigned decimal integer. |
x | int | Unsigned hexadecimal integer, using "abcdef." |
X | int | Unsigned hexadecimal integer, using "ABCDEF." |
e | double | Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –. |
E | double | Identical to the e format except that E rather than e introduces the exponent. |
f | double | Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. |
g | double | Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. |
G | double | Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). |
n | Pointer to integer | Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. |
p | Pointer to void | Prints the address of the argument in hexadecimal digits. |
s | String | When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached. |
S | String | When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached. |
2.Format Specification Fields: printf and wprintf Functions
A format specification, which consists of optional and required fields, has the following form:
%[flags] [width] [.precision] [{h | l | I | I32 | I64}]type
Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s
). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%
.
The optional fields, which appear before the type character, control other aspects of the formatting, as follows:
-
type
- Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see the printf Type Field Characters table. flags
- Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see the Flag Characters table). More than one flag can appear in a format specification. width
- Optional number that specifies the minimum number of characters output (see printf Width Specification). precision
- Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see the How Precision Values Affect Type table). h | l | I | I32 | I64
- Optional prefixes to type-that specify the size of argument (see the Size Prefixes for printf and wprintf Format-Type Specifiers table).
Security Note Ensure that format specification strings are not user-defined. For example, consider a program that prompts the user to enter his name and stores the input in a string variable called name. To print name, do not do this:
printf( name ); // Danger! If name contains "%s", program will crash
Instead, do this:
printf( "%s", name );