Parameters |
The format string may contain the following specifications: Ordinary characters (char ) These are output on a 1 : 1 basis. Control characters for white space, beginning with a backslash (\). Format statements, which begin with the percent sign (%). Data passed in an argument list is formatted and converted in accordance with the specifications in the format statements. One format statement is required per argument, with the first format statement corresponding to the first argument and so forth.
Ordinary characters All characters which are not elements of format statements and do not represent special control characters (beginning with a backslash) are output unchanged. If the percent character (%) is to be written, it must be specified twice in succession (%%). Control characters for white space \n | line feed | \t | tab | \f | form feed | \v | vertical tab | \b | backspace | \r | carriage return |
Further information on converting these control characters is given in section “Whitespace” (Basic terms). argumentlist Variables or constants whose values are to be converted and formatted for output according to the information in the format statements. If the number of format statements does not match the number of arguments the following applies: If there are more arguments, the surplus arguments are ignored. If there are fewer arguments, the results are undefined. The format statement is described separately below for KR functionality and ANSI functionality. Format statements may be structured as follows: %
| [-][+][0][n|*][.m|.*]
| { [l] {d|o|u|x} | {D|O|U|X} | {f|e|g} | {c|s|%} }
| 1. | 2. | 3. | Every format statement must begin with a percent character (%). General formatting characters, e.g. to control the output of signs, left or right justification, width of the output field, etc. Characters which specify the actual conversion.
Meaning of the formatting characters: - | Left-justified alignment of the output field. Default: right-justified alignment. | + | The result of a conversion with a sign is always output with a sign. Default: only a negative sign (if present) is output. | 0 | Pad with zeros. The output field is padded with zeros for all conversions. Default: output field is padded with blanks. Padding with zeros is also performed in the case of left-justified alignment (formatting character -). | n | Minimum total field width (including decimal point). If more digits are required for the conversion of a number, this specification has no meaning. If the output is shorter than the specified field width it is padded with blanks or zeros to make up the field width (cf. formatting characters - and 0). | * | The total field width (see n) is defined by an argument instead of in the format statement. The current value (integer) must immediately precede the argument to be converted or immediately precede the precision value (formatting character .m) in the argument list (separated by a comma). | .m | Precision. e, f, g conversion: precise number of digits after the decimal point (maximum 20). Default: 6 digits. s conversion: maximum number of characters to be output. Default: all characters up to the terminating null byte (\0). In all other conversions the precision value is ignored. | .* | The precision (see .m) is defined by an argument instead of in the format statement. The current value (integer) must immediately precede the argument to be converted in the argument list (separated by a comma). | Meaning of the conversion characters: l | l before d, o, u, x: Conversion of an argument of type long int . Identical to uppercase letters (D, O, U, X). | d, o, u, x |
| Representation of an integer (int ) as a signed decimal number (d), unsigned octal number (o), unsigned decimal number (u), unsigned hexadecimal number (x). | f | Floating-point number (float or double ) in the format [-]ddd.ddd The decimal point character is determined by the locale (category LC_NUMERIC). The default setting is the period. The number of digits after the decimal point depends on the precision specification in .m. Default (no specification): 6 digits If the precision is 0, the number is output without a decimal point. | e | Floating-point number (float or double ) in the format [-]d.ddde{+|-}dd. The decimal point character is determined by the locale (category LC_NUMERIC). The default setting is the period. The number of digits after the decimal point depends on the precision specification in .m. Default (no specification): 6 digits If the precision is 0, a decimal point with no digits after it is output. | g | Floating-point number (float or double ) in the f or e format. The number of digits after the decimal point depends on the precision specification in .m. In each case, the representation chosen is the one that requires the least space while maintaining precision. | c | Format for the output of a single character (char ). The character ’\0’ is ignored. | s | Format for the output of strings. The string should be terminated with ’\0’. printf writes as many characters of the string as is specified in .m. Default (no specification): printf writes all characters up to ’\0’. | % | Print a %, no conversion. | Format statements may be structured as follows: %
|
[-][+]['BLANK'][#][0][n|*][.m|.*]
|
{[{hh|h|l|ll|j|z|t}] {d|i|o|u|x|X} | [{hh|h|l|ll|j|z|t}] n | [L] {a|A|e|E|f|F|g|G} | [l] {c|s} | {D|O|U|p} | % }
| 1. | 2. | 3. | Every format statement must begin with a percent character (%). General formatting characters, e.g. to control the output of signs, left or right justification, width of the output field, etc. Characters which specify the actual conversion.
Meaning of the formatting characters: - | Left-justified alignment of the output field. Default: right-justified alignment. | + | The result of a conversion with a sign is always output with a sign. Default: only a negative sign (if present) is output. | 'BLANK' (blank) |
| If the first character of a signed conversion is not a sign the result is prefixed by a blank. The formatting character 'BLANK' is ignored if + is specified at the same time. | # | Conversion of the result to an alternative format. o conversion: precision is increased so that the first digit of the result is the digit 0. x or X conversion: the string 0x or 0X is prefixed to a result not equal to 0. a, A, e, E, f, F, g and G conversion: the result always contains a decimal point, even if there are no further digits (normally the result only contains a decimal point if it is followed by at least one digit). In addition, trailing zeros are not omitted for g or G conversion. The formatting character # has no effect in c, s, d, i or u conversions. | 0 | Pad with zeros. The output field is padded with zeros for all integer (d, i, o, u, x, X) and floating-point (a, A, e, E, f, F, g, G) conversions. Default: output field is padded with blanks. 0 is ignored if the 0 and - flags both appear or if a precision is specified for integer conversions. The formatting character 0 has no effect for c, p and s conversions. | n | Minimum total field width (including decimal point). If more digits are required for the conversion of a number, this specification has no meaning. If the output is shorter than the specified field width it is padded with blanks or zeros to make up the field width (cf. formatting characters - and 0). | * | The total field width (see n) is defined by an argument instead of in the format statement. The current value (integer) must immediately precede the argument to be converted or immediately precede the precision value (formatting character .m) in the argument list (separated by a comma). | .m | Precision. d, i, o, u, x resp. X conversion: minimum number of digits after the decimal point. Default: 1. e, E, f, F conversion: precise number of digits after the decimal point (maximum 20). Default: 6 digits. a, A conversion: precise number of digits after the decimal point. Default: 13 digits for long , 27 digits for long double . g resp. G conversion: maximum number of significant digits. s conversion: maximum number of characters to be output. Default: all characters up to the terminating null byte (\0). In all other conversions the precision value is ignored. | .* | The precision (see .m) is defined by an argument instead of in the format statement. The current value (integer) must immediately precede the argument to be converted in the argument list (separated by a comma). | Meaning of the conversion characters: hh | hh before d, i, o, u, x, X: conversion of an argument of type signed char or unsigned char . hh before n: the argument is of type pointer to signed char (no conversion). | h | h before d, i, o, u, x, X: conversion of an argument of type short int or unsigned short int . h before n: the argument is of type pointer to short int (no conversion). | l | l before d, i, o, u, x, X: conversion of an argument of type long int or unsigned long int . l before d, o, u is synonymous with uppercase D, O, U. l before c: The argument is of type wint_t . l before s: The argument is of type wchar_t . l beforen: The argument is of type long int (no conversion). | ll | ll before d, i, o, u, x, X : conversion of an argument of type long long int or unsigned long long int . ll beforen: the argument is of type pointer to long long int (no conversion). | j | j before d, i, o, u, x, X: conversion of an argument of type intmax_t oder uintmax_t . j before n: the argument is of type pointer to intmax_t (no conversion). | z | z before d, i, o, u, x, X: conversion of an argument of type size_t or long int . z beforen: the argument is of type pointer to long int (no conversion). | t | t before d, i, o, u, x, X: conversion of an argument of type ptrdiff_t or unsigned long int . t before n: the argument is of type pointer to ptrdiff_t (no conversion). | L | L before e, E, f, g, G: conversion of an argument of type long double . | d, i, o, u, x, X |
| Representation of an integer (int ) as a signed decimal number (d, i), unsigned octal number (o), unsigned decimal number (u), unsigned hexadecimal number (x, X). For x the lowercase letters abcdef are used, for X the uppercase letters ABCDEF. The precision value .m indicates the minimum number of digits to be output. If the value can be represented with fewer digits the result is padded with leading zeros. The default value is precision 1. If the precision is 0 and the value is 0, there is no output. | f, F | Floating-point number (float or double ) in the format [-]ddd.ddd The decimal point character is determined by the locale (category LC_NUMERIC). The default setting is the period. The number of digits after the decimal point depends on the precision specification in .m. Default (no specification): 6 digits If the precision is 0, the number is output without a decimal point. | e, E | Floating-point number (float or double ) in the format [-]d.ddde{+|-}dd. The decimal point character is determined by the locale (category LC_NUMERIC). The default setting is the period. For E conversion the exponent is preceded by the uppercase letter E. The number of digits after the decimal point depends on the precision specification in .m. Default (no specification): 6 digits If the precision is 0, the number is output without a decimal point. | g, G | Floating-point number (float or double ) in the f or e format (or in the E format for G conversion). The number of significant places depends on the precision value .m. The e or E format is only used if the exponent of the conversion result is less than -4 or greater than the specified precision. | a, A | Floating-point number (float or double ) in the format [-] 0xh.hhhh p{+|-} d. Each h represents a hexadecimal digit. The radix character is determined by the locale (category LC_NUMERIC ). The default is a period. The exponent is printed to base 2. The a conversion character will produce a number with lowercase letters a , b , c , d , e , f , x and p , the A conversion character a number with uppercase letters A , B, C , D , E , F , X and P . The number of positions after the radix character depends on the precision specified in .m; the default is 27 positions if used together with the conversion character L, 13 otherwise. If the precision is set to 0, the output will have no radix character. | c | Without conversion character l : the argument of type int is converted to an unsigned char , and the resulting byte is written. The character ’\0’ is ignored. With conversion character l : the wint_t argument is converted as if by an ls conversion specification with no precision and an argument that points to a two-element array of type wchar_t, the first element of which contains the wint_ t argument to the ls conversion specification and the second element contains a null wide character. | p | Conversion of an argument of type pointer to void . The output is an 8-digit hexadecimal number (analogous to %08.8x). | s | Without conversion character l : Format for the output of strings. The string should be terminated with ’\0’. printf writes as many characters of the string as is specified in .m. Default (no specification): printf writes all characters up to (but not including) ’\0’. With conversion character l : the argument shall be a pointer to an array of type wchar_t. Wide characters from the array shall be converted to characters (each as if by a call to the wcrtomb function, with the conversion state described by an mbstate_t object initialized to zero before the first wide character is converted) up to and including a terminating null wide character. The resulting characters shall be written up to (but not including) the terminating null character (byte). If no precision is specified, the application shall ensure that the array contains a null wide character. If a precision is specified, no more than that many characters (bytes) shall be written (including shift sequences, if any), and the array shall contain a null wide character if, to equal the character sequence length given by the precision, the function would need to access a wide character one past the end of the array. In no case a partial character is written. | n | There is no conversion or output of the argument. The argument is of type pointer to int . This integer variable is assigned the number of characters that printf has generated for output up to this time. | %
| The % character is output; no argument is converted. |
|