Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Replaced lists of conversion specifications and length modifiers with a comprehensive table. Removed text about class ranges (there's no such thing in printf format strings).

Common mistakes in creating format strings include

  • using invalid conversion specifications
  • using a length modifier on an incorrect specification
  • mismatching the argument and conversion specification type
  • using an argument of type other than int for width, precision, or the n conversion specification
  • providing insufficient arguments for all the conversion specifications in the format string
  • using invalid character classes

Wiki Markup
The following are C99-compliant conversion specifications formatted IO functions {{fprintf()}}, {{printf()}}, {{sprintf()}}, {{snprintf()}}, {{vfprintf()}}, {{vprintf()}}, {{vsprintf()}}, and {{vsnprintf()}} convert, format, and print their arguments under control of a _format_ string. According to \[[ISO/IEC 9899:1999| AA. C References#ISO/IEC 9899-1999]\]. Using any other specification may result in [undefined behavior|BB. Definitions#undefined behavior].

Code Block

d, i, o, u, x, X, f, F, e, E, g, G, a, A, c, s, p, n, %

In addition, ISO/IEC 9945-2003 permits the following conversion specifications as synonyms for lc and ls:

Code Block

C, S

Only some of the conversion specifications can correctly take a length modifier. Using a length modifier on any specification other than the following may result in undefined behavior.

Code Block

d, i, o, u, x, X, a, A, e, E, f, F, g, G, c, s

Character class ranges must also be properly specified with a hyphen in between two printable characters. The two following lines are both properly specified. The first accepts any character from a to z, inclusive, while the second accepts anything that is not a to z, inclusive.

Code Block

[a-z]
[^a-z]

...

Programming Languages---C]\]:

The format is a character string, beginning and ending in its initial shift state, if any. The format is composed of zero or more directives: ordinary characters, which are simply copied to the output stream, and conversion specifications, each of which shall result in the fetching of zero or more arguments.

Each conversion specification is introduced by the '%' character followed by the following (in order):

  • Zero or more flags (in any order), which modify the meaning of the conversion specification.
  • An optional minimum field width.
  • An optional precision that gives the minimum number of digits to appear for certain conversion specifiers.
  • An optional length modifier that specifies the size of the argument.
  • A conversion specifier character that indicates the type of conversion to be applied.

Common mistakes in creating format strings include:

  • providing insufficient arguments for the format string
  • using invalid conversion specifiers
  • using a flag character that is incompatible with the conversion specifier
  • using a length modifier that is incompatible with the conversion specifier
  • mismatching the argument type and conversion specifier
  • using an argument of type other than int for width or precision

The following table summarizes C99-compliant conversion specifiers along with the flag characters (the apostrophe ('), -, +, the space character, and # in columns 2, through 5) and length modifiers (h, hh, l, ll, j, z, t, and L in columns 6 through 13) valid for each specification, and the type of the expected argument. Valid and meaningful combinations of a conversion specification, flag character, and length modifier is denoted by the (tick) symbol in the corresponding cell or by the name of the type argument effected by the length modifier. Valid combinations that have no effect are denoted by N/E. Using a combination of a conversion specification, flag character, and length modifier denoted by the (error) symbol or a specification not listed in the table, or an argument of an unexpected type may result in undefined behavior.

Conversion
Specifier
Character

' XSI

-
+
SPACE


#


0

 


h


hh


l


ll


j


z


t


L

Argument
Type

d, i

(tick)

(tick)

(error)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

signed integer

o

(error)

(tick)

(tick)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned integer

u

(tick)

(tick)

(error)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned integer

x

(error)

(tick)

(tick)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned integer

X

(error)

(tick)

(tick)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

unsigned integer

f, F

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

e, E

(error)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

g, G

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

a, A

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

c

(error)

(tick)

(error)

(error)

 

(error)

(error)

wint_t

(error)

(error)

(error)

(error)

(error)

int or wint_t

s

(error)

(tick)

(error)

(error)

 

(error)

(error)

NTWS

(error)

(error)

(error)

(error)

(error)

NTBS or NTWS

p

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

void*

n

(error)

(tick)

(error)

(error)

 

short*

char*

long*

long long*

intmax_t*

size_t*

ptrdiff_t*

(error)

pointer to integer

C XSI

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

wint_t

S XSI

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

NTWS

%

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

none

Legend:

  • SPACE – the space (' ') character
  • N/E – No Effect
  • NTBS – char* argument pointing to a Null-Terminated Byte String
  • NTWS – wchar_t* argument pointing to a Null-Terminated Wide character String
  • XSI – ISO/IEC 9945-2003 XSI extension

Noncompliant Code Example

...