printf Format Specifiers

From Appmethod Topics
Jump to: navigation, search

Go Up to printf, wprintf


Print format specifiers have the following form:

% [flags] [width] [.prec] [F|N|h|l|L] type_char

Each format specifier begins with the percent character (%).

Optional Format String Components

These are the general aspects of output formatting controlled by the optional characters, specifiers, and modifiers in the format string:


Component    Optional/Required What It Is/Does

[flags]

(Optional)

They can appear in any order and combination.

  • - = Left-justifies the result, pads on the right with blanks. If not given, it right-justifies the result, pads on the left with zeros or blanks.
  • + = Signed conversion results always begin with a plus (+) or minus (-) sign.
  • blank = If value is non-negative, the output begins with a blank instead of a plus; negative values still begin with a minus.
  • # = Specifies that arg is to be converted using an alternate form.

Note: Plus (+) takes precedence over blank () if both are given.

[width]

(Optional)

The width specifier sets the minimum field width for an output value.

Width is specified in one of two ways:

  • Directly, through a decimal digit string.
  • Indirectly, through an asterisk (*).

If you use an asterisk for the width specifier, the next argument in the call (which must be an int) specifies the minimum output field width.

Nonexistent or small field widths do not cause truncation of a field. If the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

How output width is affected

n = At least n characters are printed. If the output value has less than n characters, the output is padded with blanks (right-padded if the flag is given, left-padded otherwise).

0n = At least n characters are printed. If the output value has less than n characters, it is filled on the left with zeros.

'*' = The argument list supplies the width specifier, which must precede the actual argument being formatted.

[prec]

(Optional)

Precision specifier. Maximum number of characters to print; for integers, minimum number of digits to print.

[F|N|h|l|L]

(Optional)

These modifiers determine how printf functions interpret the next input argument, arg[f].

Prefix Format specifier Type specified

F

p s

A far pointer

N

n

A near pointer

h

d i o u x X

A short int

l

d i o u x X

A long int

l

e E f g G

A double

L

e E f g G

A long double

L

d i o u x X

An __int64

h

c C

A single-byte character

l

c C

A Wide character

h

s S

A single-byte character string

l

s S

A Wide character string

These modifiers affect how all the printf functions interpret the data type of the corresponding input argument arg. Both F and N reinterpret the input variable arg. Normally, the arg for a %p, %s, or %n conversion is a pointer of the default size for the memory model. h, l, and L override the default size of the numeric data input arguments. Neither h nor l affects pointer (p,n) types.

type_char

(Required)

Type character


See Also