Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added commong mistakes, changed "conversion specifier" to "conversion specification," added POSIX C and S, and the missing C99 c and s conversion specifications to the set of length modifiers.

Common mistakes in creating format strings include

  • using invalid conversion specifiersspecifications
  • using a length modifier on an incorrect specifierspecification
  • mismatching the argument and conversion specifier typespecification 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 specifiersspecifications \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\]. Using any other specifierspecification 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 specifiers specifications can correctly take a length modifier. Using a length modifier on any specifier 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.

...

Mismatches between arguments and conversion specifiers specifications may result in undefined behavior. Many compilers can diagnose type mismatches in formatted output function invocations.

...

This compliant solution ensures that the format arguments match their respective format specifiersspecifications.

Code Block
bgColor#ccccff
const char *error_msg = "Resource not available to user.";
int error_type = 3;
/* ... */
printf("Error (type %d): %s\n", error_type, error_msg);

...