Format Specifiers
Home ] Up ] Classes Involved ] [ Format Specifiers ] Some Examples ]

 

 

What's a Format Specifier?  Here's the scoop:

A call to one of the methods in the printf family involves passing in a Format String, which contains:

  • Normal text

plus

  • Zero or more Format Specifiers

In most cases, a Format Specifier is associated with one of the arguments passed in after the Format String argument.  A Format Specifier, as its name suggests, specifies the output format for how that argument's value should be displayed.

General, Character and Numeric Type Format Specifiers

The format specifiers for general, character, and numeric types have the following syntax:

%[argument_index$][flags][width][.precision]conversion

The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.

The optional width is a non-negative decimal integer indicating the minimum number of characters to be written to the output.

The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.

The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.

Date and Time Format Specifiers

The format specifiers for types which are used to represents dates and times have the following syntax:
%[argument_index$][flags][width]conversion

The optional argument_index, flags and width are defined as above.

The required conversion is a two character sequence. The first character is 't' or 'T'. The second character indicates the format to be used. These characters are similar to but not completely identical to those defined by GNU date and POSIX strftime(3c).

Non-Argument Format Specifiers

The format specifiers which do not correspond to arguments have the following syntax:
%[flags][width]conversion

The optional flags and width is defined as above.

The required conversion is a character indicating content to be inserted in the output.

 
The page was last updated February 19, 2008