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 then
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 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 symbol or a specification not listed in the table, or an argument of an unexpected type may result in undefined behavior.
Conversion | | |
|
|
|
|
|
|
|
|
|
|
| Argument |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
|
|
| | | | | | | |
| signed integer |
|
|
|
|
|
| | | | | | | |
| unsigned integer |
|
|
|
|
|
| | | | | | | |
| unsigned integer |
|
|
|
|
|
| | | | | | | |
| unsigned integer |
|
|
|
|
|
| | | | | | | |
| unsigned integer |
|
|
|
|
|
|
|
| N/E | N/E |
|
|
| | |
|
|
|
|
|
|
|
| N/E | N/E |
|
|
| | |
|
|
|
|
|
|
|
| N/E | N/E |
|
|
| | |
|
|
|
|
|
|
|
| N/E | N/E |
|
|
| | |
|
|
|
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
|
|
|
| NTWS |
|
|
|
|
| NTBS or NTWS |
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
| | | | | | | |
| pointer to integer |
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
| NTWS |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
...