Format Control String
The format-control string used when formatting the string value of this item, based on its external item(s)
A format specification, which consists of optional and required fields, has the following form:
%[flags] [width] [.precision] [{h | l | ll | 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 "Type Field Characters" table in Type Field Characters).
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 in Flag Directives). More than one flag can appear in a format specification.
width
Optional number that specifies the minimum number of characters output (see 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 in Precision Specification).
h| l| ll| I| I32| I64
Optional prefixes to type-that specify the size of argument (see the "Size Prefixes" table in Size and Distance Specification).
Flag Characters
Flag | Meaning | Default |
---|---|---|
Left align the result within the given field width. | Right align. | |
+ | Prefix the output value with a sign (+ or ) if the output value is of a signed type. | Sign appears only for negative signed values (). |
0 | If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) and a precision specification is also present (for example, %04.d), the 0 is ignored. | No padding. |
blank (' ) | Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. | No blank appears. |
# | When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively. | No blank appears. |
When used with the e, E, f, a or A format, the # flag forces the output value to contain a decimal point in all cases. | Decimal point appears only if digits follow it. | |
When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros. Ignored when used with c, d, i, u, or s. | Decimal point appears only if digits follow it. Trailing zeros are truncated. |
Type Field Character
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 | Signed 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]dd[d] where d is a single decimal digit, dddd is one or more decimal digits, dd[d] is two or three decimal digits depending on the output format and size of the exponent, 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). |
a | double | Signed hexadecimal double precision floating point value having the form []0xh.hhhh p±dd, where h.hhhh are the hex digits (using lower case letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point. |
A | double | Signed hexadecimal double precision floating point value having the form []0Xh.hhhh P±dd, where h.hhhh are the hex digits (using capital letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point. |
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. See Security Note below. |
p | Pointer to void | Prints the address of the argument in hexadecimal digits. |
s | String | When used with printf functions, specifies a single-bytecharacter 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-bytecharacter string. Characters are printed up to the first null character or until the precision value is reached. |
t T | Time | Used to format Time object, terminate with &t or &T. see Time format %T%Y.%#m.%d %H:%M:%S&T output: 2006.9.06 18:02:21 |
How Precision Values Affect Type
Type | Meaning | Default |
---|---|---|
a, A | The precision specifies the number of digits after the point. | Default precision is 6. If precision is 0, no point is printed unless the # flag is used. |
c, C | The precision has no effect. | Character is printed. |
d, i, u, o, x, X | The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. | Default precision is 1. |
e, E | The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. | Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed. |
f | The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. | Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed. |
g, G | The precision specifies the maximum number of significant digits printed. | Six significant digits are printed, with any trailing zeros truncated. |
s, S | The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. | Characters are printed until a null character is encountered. |
Width SpecificationCharacters
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values depending on whether the flag (for left alignment) is specified until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.
Size Prefixes for printf and wprintf Fomat-Type Specifiers
To specify | Use prefix | With type specifier |
---|---|---|
long int | l (lowercase L) | d, i, o, x, or X |
long unsigned int | l | o, u, x, or X |
long long | ll | d, i, o, x, or X |
short int | h | d, i, o, x, or X |
short unsigned int | h | o, u, x, or X |
__int32 | I32 | d, i, o, x, or X |
unsigned __int32 | I32 | o, u, x, or X |
__int64 | I64 | d, i, o, x, or X |
unsigned __int64 | I64 | o, u, x, or X |
ptrdiff_t (that is, __int32 on 32-bit platforms, __int64 on 64-bit platforms) | I | d, i, o, x, or X |
size_t (that is, unsigned __int32 on 32-bit platforms, unsigned __int64 on 64-bit platforms) | I | o, u, x, or X |
long double | l or L | f |
Single-byte character with printf functions | h | c or C |
Single-byte character with wprintf functions | h | c or C |
Wide character with printf functions | l | c or C |
Wide character with wprintf functions | l | c or C |
Single-byte character string with printf functions | h | c or C |
Single-byte character string with wprintf functions | h | c or C |
Wide-character string with printf functions | l | c or C |
Wide-character string with wprintf functions | l | c or C |
Wide character | w | c |
Wide-character string | w | s |
Thus to print single-byte or wide-characters, use format specifiers as follows.
To print charachter as | Use function | With format specifier |
---|---|---|
single byte | printf | c, hc, or hC |
single byte | wprintf | C, hc, or hC |
wide | wprintf | c, lc, lC, or wc |
wide | printf | C, lc, lC, or wc |
Time format arguments
The format argument consists of one or more codes; as in printf, the formatting codes are preceded by a percent sign (%). Characters that do not begin with % are copied unchanged
The formatting codes for are listed below:
Format code | Meaning |
---|---|
%a | Abbreviated weekday name |
%A | Full weekday name |
%b | Abbreviated month name |
%B | Full month name |
%c | Date and time representation appropriate for locale |
%d | Day of month as decimal number (01 31) |
%H | Hour in 24-hour format (00 23) |
%I | Hour in 12-hour format (01 12) |
%j | Day of year as decimal number (001 366) |
%m | Month as decimal number (01 12) |
%M | Minute as decimal number (00 59) |
%p | Current locale's A.M./P.M. indicator for 12-hour clock |
%S | Second as decimal number (00 59) |
%U | Week of year as decimal number, with Sunday as first day of week (00 53) |
%w | Weekday as decimal number (0 6; Sunday is 0) |
%W | Week of year as decimal number, with Monday as first day of week (00 53) |
%x | Date representation for current locale |
%X | Time representation for current locale |
%y | Year without century, as decimal number (00 99) |
%Y | Year with century, as decimal number |
%z, %Z | Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown |
%% | Percent sign |
As in the printf function, the # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.
Fomrat code | Meaning |
---|---|
%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% | # flag is ignored. |
%#c | Long date and time representation, appropriate for current locale. For example: "Tuesday, March 14, 1995, 12:41:29". |
%#x | Long date representation, appropriate to current locale. For example: "Tuesday, March 14, 1995". |
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y | Remove leading zeros (if any). |