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

FlagMeaningDefault
 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 ().
0If 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

CharacterTypeOutput format
cint or wint_tWhen used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character
Cint or wint_tWhen used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character.
dintSigned decimal integer.
iintSigned decimal integer.
ointSigned octal integer.
uintUnsigned decimal integer.
xintUnsigned hexadecimal integer, using "abcdef."
XintUnsigned hexadecimal integer, using "ABCDEF."
edoubleSigned 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 .
EdoubleIdentical to the e format except that E rather than e introduces the exponent.
fdoubleSigned 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.
gdoubleSigned 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.
GdoubleIdentical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
adoubleSigned 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.
AdoubleSigned 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.
nPointer to integerNumber 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.
pPointer to voidPrints the address of the argument in hexadecimal digits.
sStringWhen 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.
SStringWhen 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 TTimeUsed 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

TypeMeaningDefault
a, AThe 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, CThe precision has no effect.Character is printed.
d, i, u, o, x, XThe 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, EThe 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.
fThe 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, GThe precision specifies the maximum number of significant digits printed.Six significant digits are printed, with any trailing zeros truncated.
s, SThe 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 specifyUse prefixWith type specifier
long intl (lowercase L)d, i, o, x, or X
long unsigned intlo, u, x, or X
long longlld, i, o, x, or X
short inthd, i, o, x, or X
short unsigned intho, u, x, or X
__int32I32d, i, o, x, or X
unsigned __int32I32o, u, x, or X
__int64I64d, i, o, x, or X
unsigned __int64I64o, u, x, or X
ptrdiff_t (that is, __int32 on 32-bit platforms, __int64 on 64-bit platforms)Id, i, o, x, or X
size_t (that is, unsigned __int32 on 32-bit platforms, unsigned __int64 on 64-bit platforms)Io, u, x, or X
long doublel or Lf
Single-byte character with printf functionshc or C
Single-byte character with wprintf functionshc or C
Wide character with printf functionslc or C
Wide character with wprintf functionslc or C
Single-byte character string with printf functionshc or C
Single-byte character string with wprintf functionshc or C
Wide-character string with printf functionslc or C
Wide-character string with wprintf functionslc or C
Wide characterwc
Wide-character stringws

Thus to print single-byte or wide-characters, use format specifiers as follows.

To print charachter asUse functionWith format specifier
single byteprintfc, hc, or hC
single bytewprintfC, hc, or hC
widewprintfc, lc, lC, or wc
wideprintfC, 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 codeMeaning
%aAbbreviated 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 codeMeaning
%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#%# flag is ignored.
%#cLong date and time representation, appropriate for current locale. For example: "Tuesday, March 14, 1995, 12:41:29".
%#xLong date representation, appropriate to current locale. For example: "Tuesday, March 14, 1995".
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#YRemove leading zeros (if any).