Description | fprintf() writes formatted output on the output stream pointed to by stream.
printf() writes formatted output on the standard output stream stdout .
sprintf() writes formatted output, followed by the null byte, in consecutive bytes starting at the address s. The user must ensure that sufficient space is available.
Each of these functions converts the arguments in arglist and outputs them under the control of the format. format is a character string, beginning and ending in its initial shift state, if defined. It is composed of zero or more directives and may include the following three types of characters: - characters of type
char , which are simply copied to the output stream (1: 1). - white-space characters, starting with a backslash (
\ ) (see isspace() ). - conversion specifications beginning with the percent character (
% ), each of which is associated with zero or more arguments in arglist. The results are undefined if fewer arguments are passed in arglist than are defined in format. If the number of arguments defined in format is greater than the arguments passed in arglist, the excess arguments are ignored.
The following applies to the current version of the C runtime system: Only characters from the EBCDIC character set are permitted. Character | Meaning | Format control action | \b
| backspace character | The output is shifted to 1 character before the current position, unless the current position is the start of a line In this version of the C runtime system, \b is evaluated only for BS2000 output, not for output to the POSIX subsystem. | \f
| form-feed character | The output is shifted to the start of the next logical page. In this version of the C runtime system, \f is evaluated only for BS2000 output, not for output to the POSIX subsystem.. | \n
| newline character | The output is shifted to the start of the next line. | \r
| carriage return | The output is shifted to the start of the current line. All output that was already written on the stream of this line is discarded. | \t
| horizontal tab | The output is shifted to 8 characters after the current position. | \v
| vertical tab | The output is shifted to the next vertical tab position. In this version of the C runtime system, \v is evaluated only for BS2000 output, not for output to the POSIX subsystem. | Conversions can be applied to the n-th argument after the format in the argument list, rather than to the next unused argument. In this case, the conversion character % is replaced by the sequence % n $ , where n is a decimal integer in the range [1, {NL_ARGMAX}] , giving the position of the argument in the argument list. This feature provides for the definition of format strings that select arguments in an order appropriate to specific languages. In format strings containing the % n $ form of conversion specifications, elements in the argument list arglist can be referenced from the format string format as many times as required (n-times). In format strings containing the % form of conversion specifications, each argument in the argument list is evaluated exactly once. All forms of fprintf() allow for the insertion of a language-dependent radix character in the output string. The radix character is defined in the program ́s locale (category LC_NUMERIC ). In the POSIX locale, or in a locale where the radix character is not defined, the radix character defaults to a period (.). Each conversion specification is introduced by the % character or by the character sequence % n $ , after which the following appear in sequence: Zero or more flags, which modify the meaning of the conversion specification. An optional decimal number or "*" that specifies a minimum field width. If the converted value has fewer bytes than the field width, it will be padded to the field width with spaces on the left (or padded on the right if the left-adjustment flag "-" was specified). If "*" is specified, the total field width is defined by an integral argument that must immediately precede the argument to be converted or the value of the precision specification (flag ".*") in the argument list.
A precision that gives the minimum number of digits to appear for the d , i , o , u , x and X conversions; the number of digits to appear after the radix character for the e , E and f conversions; the maximum number of significant digits for the g and G conversions; or the maximum number of bytes to be printed from a string in s conversion. The precision takes the form of a period (.), followed by a decimal digit string, where a null digit string is treated as 0 or the form ".*" if the precision is passed as an argument immediately preceding the argument to be converted. An optional length modifier that specifies the size of the argument. If a length modifier appears with any other conversion character than specified in the conversion characters table, the behavior is undefined. A conversion character that indicates the type of conversion to be applied.
A field width, or precision, or both, may be indicated by an asterisk ( *). In this case an argument of type int supplies the field width or precision. Arguments specifying field width, or precision, or both must appear in that order before the argument, if any, to be converted. A negative field width is taken as a "-" flag followed by a positive field width. A negative precision is taken as if the precision were omitted. In format strings containing the % n $ form of a conversion specification, a field width or precision may be indicated by the sequence *m $ , where m s a decimal integer in the range [1, {NL_ARGMAX}] giving the position in the argument list of an integer argument containing the field width or precision, for example: printf ("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
format can contain either numbered argument specifications (that is, % n $ and % m $ ), or unnumbered argument specifications (that is, % and *), but normally not both. The results of mixing numbered and unnumbered argument specifications in a format string are undefined. When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string. Conversion specifications can be given in XPG4 Version 2-conformant environments as shown below: %[a$] | ['][-][+]['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} |
{C|S} |
{D|O|U|p} |
% } | 1. | 2. | 3. | 4. | 5. |
Start of a conversion specification Flags Field width Precision Characters that define the actual conversion
'
| The integer portion of the result of a decimal conversion (%i, %d, %u, %f, F, %g or %G ) will be formatted with thousands grouping characters. For other conversions, the behavior is undefined. The non-monetary grouping character is used. | -
| The result of the conversion will be left-justified within the field. | +
| The result of a signed conversion will always begin with a sign (+ or -). | 'BLANK' | If the first character of a signed conversion is not a sign, a space will be prefixed to the result. This means that if the space and + flags both appear, the space flag will be ignored. | #
| This flag specifies that the value is to be converted to an alternative form. This flag has no effect for c , d , i , s and u. For o conversion, it increases the precision to force the first digit of the result to be 0. For x or X conversions, a non-zero result will have the string "0x" (or "0X" ) prefixed to it. For a , A, e, E , f , F , g or G conversions, the result will always contain a radix character, even if no digits follow the radix character. Without this flag, a radix character appears in the result of these conversions only if a digit follows it. For g and G conversions, trailing zeros will not be removed from the result as they normally are. | 0
| For d , i , o , u , x , X , e , E , f , g and G conversions, leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed. If the 0 and - flags both appear, the 0 flag will be ignored. For d , i , o , u , x and X conversions, if a precision is specified, the 0 flag will be ignored. For other conversions, the behavior is undefined. |
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 equivalent to the uppercase letters D , O , U .
l before c : conversion of an argument of type wint_t (equivalent to C ).
l before s : conversion of an argument of type wchar_t (equivalent to C ).
l before n : the argument is of type pointer to 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 before n: 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 or 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 before n : 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 a , A , e , E , f , F , g , G : conversion of an argument of type long double .
| d , i
| The int argument is converted to a signed decimal in the style [-]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters. | o
| The unsigned int argument is converted to unsigned octal format in the style dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters. | u
| The unsigned int argument is converted to unsigned decimal format in the style dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters. | x
| The unsigned int argument is converted to unsigned hexadecimal format in the style dddd; the letters abcdef are used in addition to the digits. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters. | X
| Behaves the same as the x conversion character except that letters ABCDEF are used. | f , F
| The double argument is converted to decimal notation in the style [-] ddd. ddd, where the number of digits after the radix character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly 0 and no # flag is present, no radix character appears. If a radix character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. | e , E
| The double argument is converted in the style [-] d.ddde+-dd, where there is one digit before the radix character (which is non-zero if the argument is non-zero) and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is 0 and no # flag is present, no radix character appears. The value is rounded to the appropriate number of digits. The E conversion character will produce a number with E instead of e introducing the exponent. The exponent always contains at least two digits. If the value is 0, the exponent is 0. | g , G
| The double argument is converted in the style f or e (or in the style E in the case of a G conversion character), with the precision specifying the number of significant digits. If an explicit precision is 0, it is taken as 1. The style used depends on the value converted; style e (or E ) will be used only if the exponent resulting from such a conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result; a radix character appears only if it is followed by a digit. | a , A
| The double argument (float oder double ) is converted in the style[-] 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
| The int argument is converted to an unsigned char , and the resulting byte is written. | s
| The argument must be a pointer to an array of char . Bytes from the array are written up to (but not including) any terminating null byte. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array must contain a null byte. | p
| The argument must be a pointer to void . The value of the pointer is converted to a sequence of printable characters; in the POSIX subsystem, this is the hexadecimal representation of the address. | n
| The argument must be a pointer to an integer into which is written the number of bytes written to the output so far by this call to one of the printf functions. No argument is converted. | C
| wchar_t is converted to an array of bytes representing a character, and the resulting character is written. If the precision is specified, the effect is undefined. The conversion is the same as that expected from wctomb() . This version of the C runtime system only supports 1-byte characters as wide character codes. They are of type wchar_t (see stddef.h ). This conversion character has therefore no effect.
| S
| The argument must be a pointer an array of type wchar_t . Wide character codes from the array, up to but not including any terminating null widecharacter code are converted to a sequence of bytes, and the resulting bytes are written. If the precision is specified, no more than that many bytes are written, and only complete characters are written. If the precision is not specified, or is greater than the size of the array of converted bytes, the array of wide characters must be terminated by a null wide character. The conversion is the same as that expected from wcstombs() . This version of the C runtime system only supports 1-byte characters as wide character codes. They are of type wchar_t (see stddef.h ). This conversion character has therefore no effect. | %
| The % character is output; no argument is converted. |
If the character that follows % or the character sequence % a $ is not a valid conversion character, the result of the conversion is undefined. In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is simply expanded to contain the conversion result. Characters generated by printf() and fprintf() are printed as if putc() had been called. The structure components st_ctime and st_mtime of the file are marked for changing between successful execution of fprintf() or printf() and the next successful completion of a call to fflush() or fclose() for the same data stream or a call to exit() or abort() (see sys/stat.h ). BS2000 The conversion specifications for output to STDOUT depend on whether KR (only available with C/C++ versions lower than V3) or ANSI functionality is to be supported. The appropriate specifications for both functionalities are detailed below. (End) BS2000 Conversion specifications (KR functionality) (only available with C/C++ versions lower than V3) The conversion specifications may be entered in the following format: %
| [-][+][0][ n | *][. m | .*]
| { [l] {d|o|u|x} |
{D|O|U|X} |
{f|e|g} |
{c|s}
% }
| 1. | 2. | 3. |
Every conversion specification must begin with a percent character (% ). Flags (i.e. formatting characters) to control the output of a sign, left or right justification, width of the output field, etc. Characters that define the actual conversion.
Meanings of flags for KR functionality (only available with C/C++ versions lower than V3): -
| Left-justified alignment of the output field. Default: right-justified alignment. | +
| The result of a signed conversion will always be output with a sign. Default: only a negative sign, if present, is output. | 0
| Zero padding. The output field will be padded with zero for all conversions. Default: The output field is padded with blanks. Zero padding will also be used with left-justified alignment (flag -). | n | Minimum field width (including radix character). If more positions are required for the conversion of a number, this specification has no effect. If the output is shorter than the specified field width, it is padded with blanks or zeros up to the field width (see flags - and 0). | *
| The total field width (see n) is defined by an argument instead of a conversion specification. The current (integral) value must immediately precede the argument to be converted or the value of the precision specification (flag .m) in the argument list (delimited by a comma). | . m
| Precision specification.
e , f , g conversions: exact number of positions after the radix character. Default: 6 positions.
s conversion: maximum number of characters to be output. Default: all characters up to the terminating null byte. The precision specification is ignored for all other conversions. | .*
| The precision (see .m) is defined by an argument instead of a conversion specification. The current (integral) value must immediately precede the argument to be converted in the argument list (delimited by a comma). |
Meanings of conversion characters for KR functionality (only available with C/C++ versions lower than V3): l
| l before d, o , u , x : conversion of an argument of type long . This specification is identical to be uppercase letters D , O , U , X .
| d , o , u , x | Representation of an integer (int ) as signed decimal number (d ), unsigned octal number (o ), unsigned decimal number (u ), signed hexadecimal number (x) . | f
| Representation of a floating-point number (float or double ) in the form [-] ddd.ddd. The radix character is determined by the locale (category LC_NUMERIC ). The default is a period. The number of positions after the radix character depends on the precision specified in .m; the default is 6 positions. If the precision is set to 0, the output will have no radix character. | e
| Representation of a floating-point number (float or double ) in the form [-] d.ddd e{+|-} dd. The radix character is determined by the locale (category LC_NUMERIC ). The default is a period. The number of positions after the radix character depends on the precision specified in .m; the default is 6 positions. If the precision is set to 0, the output will include the radix character, but with no following digits. | g
| Representation of a floating-point number (float or double ) in f or e form. The number of positions after the radix character depends on the precision specified in .m; the representation requiring the least space while maintaining the precision is selected. | c
| Format for the output of a single character (char ). The null byte is ignored. | s
| Format for the output of strings. The printf functions write the same number of characters of the string as are specified in the precision .m. Default: all characters up to (but not including) a terminating null byte are written by printf() functions. | %
| Output of the character % , without conversion. (End) |
BS2000 Conversion specifications ANSI functionality) The conversion specifications may be entered in the following format: : %
| [-][+]['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 conversion specification must begin with a percent character (% ). Flags (i.e. formatting characters) to control the output of a sign, left or right justification, width of the output field, etc. Characters that define the actual conversion.
Meanings of flags (for ANSI functionality): -
| Left-justified alignment of the output field. Default: right-justified alignment. | +
| The result of a signed conversion will always be output with a sign. Default: only a negative sign, if present, is output. | 'BLANK'
| If the first character of a signed string to be converted is not a sign, the result is prefixed by a blank. The flag 'BLANK' is ignored if + is specified at the same time. | #
| Conversion of the result to an alternative form.
For o conversion, the precision is increased to force the first digit of the result to be 0. For x or X conversions, a non-zero result will have the string 0x or 0 prefixed to it. For a -, A -, e -, E -, f -, F , g - or G conversions, the result will always contain a radix character, even if no digits follow the radix character (a radix character normally appears in the result of these conversions only if a digit follows it). Furthermore, for g and G conversions, trailing zeros will not be removed from the result. The flag # has no effect with c , s , d , i and u conversions. | 0 | Zero padding. The output field is padded with zeros on converting integers (d , i , o , u , x , X ) and floating-point numbers (a , A , e , E , f , F , g , G ). By default, the output field is padded with blanks. 0 is ignored if the flag or a precision .m is specified when converting integers. The 0 flag has no effect with c , p and s conversions. | n | Minimum total field width (including radix character). If more positions are required for the conversion of a number, this specification has no effect. If the output is shorter than the specified field width, it is padded with blanks or zeros up to the field width (see flags - and 0). | *
| The total field width (see n) is defined by an argument instead of a conversion specification. The current (integral) value must immediately precede the argument to be converted or the value of the precision specification (flag .m) in the argument list (delimited by a comma). | . m
| Precision specification.
d , i , o , u , x or X conversions: minimum number of digits to be output. Default: 1.
e , E , f and F conversions: exact number of positions after the radix character (max 20). Default: 6 positions.
a , A conversions: exact number of positions after the radix character. Default: 13 positions for double , 27 positions for long double .
g or G conversions: maximum number of significant positions.
s conversions: maximum number of characters to be output. Default: all characters up to the terminating null byte (\0). | .*
| The precision (see .m) is defined by an argument instead of a conversion specification. The current (integral) value must immediately precede the argument to be converted in the argument list (delimited by a comma). |
Meanings of conversion characters (for ANSI functionality): 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 equivalent to the uppercase letters D , O , U .
l before c : conversion of an argument of type wint_t .
l before s : conversion of an argument of type wchar_t .
l before n : the argument is of type pointer to 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 before n: 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 oder long int . z before n : 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 oder unsigned long int .
t before n : the argument is of type pointer to ptrdiff_t (no conversion).
| L
| L before a , A , e , E , f , F , g , G : conversion of an argument of type long double .
| d , i , o , u , x , X | Representation of an integer (int ) as signed decimal number (d, i ), unsigned octal number (o ), unsigned decimal number (u ), unsigned hexadecimal number (x, X ). The lowercase letters abcdef are used with x , and the uppercase letters ABCDEF are used with X . The precision specification .m defines the minimum number of digits to be output. If the value can be represented using fewer digits, the result will be padded with leading zeros. A precision of 1 is set by default. The result of converting the value 0 with precision 0 is no output. | f , F
| Representation of a floating-point number (float or double ) in the form [-] ddd.ddd. The radix character is determined by the locale (category LC_NUMERIC ). The default is a period. The number of positions after the radix character depends on the precision specified in .m; the default is 6 positions. If the precision is set to 0, the output will have no radix character. | e , E
| Representation of a floating-point number (float or double ) in the form [-] d.ddd e{+|-} dd. The radix character is determined by the locale (category LC_NUMERIC ). The default is a period. For E conversions, the exponent is prefixed by the uppercase letter E . The number of positions after the radix character depends on the precision specified in .m; the default is 6 positions. If the precision is set to 0, the output will have no radix character. | g , G
| Representation of a floating-point number (float or double ) in f or e form (or for G conversions, in E form). The number of significant positions defects on the precision specified in .m. The e or E f orm is used only if the exponent of the conversion result is less than -4 or greater than the specified precision. | a , A
| Representation of a floating-point number (float or double ) in the form [-] 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 include the output will have no radix character. | c
| Without conversion character l : The int argument is converted to an unsigned char, and the resulting byte is written. 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 occurs as an 8-digit hexadecimal number (analogous to the entry %08.8x ). | s
| Without conversion character l : The argument is a pointer to an array of char . Bytes from the array are written up to (but not including) any terminating null byte. If the precision .m is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the application shall ensure that the array contains a null byte. With conversion character l : The argument is a pointer to an array of type wchar_t . Wide characters from the array are 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 are written up to (but not including) the terminating null 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) are written. In no case a partial character is written. | n
| No conversion and output of the argument occurs. The argument is of type pointer to int . This integer variable is assigned the number of bytes that were generated for output by the printf functions up to that point. | %
| Output of the character % , without conversion. (End) |
|