Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

printf - Formatted output on standard output

&pagelevel(4)&pagelevel


Definition

#include <stdio.h>

int printf(const char *format, argumentlist);

printf edits data (characters, strings, numerical values) for output according to the specifications in the string format and writes this data to the standard output stdout. Numeric values are converted from their internal representation into printable characters in the process.

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 statement (KR functionality, applies to C/C++ versions prior to V3.0 only)

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.

  1. Every format statement must begin with a percent character (%).

  2. General formatting characters, e.g. to control the output of signs, left or right justification, width of the output field, etc.

  3. 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 statement (ANSI functionality)

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.

  1. Every format statement must begin with a percent character (%).

  2. General formatting characters, e.g. to control the output of signs, left or right justification, width of the output field, etc.

  3. 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, DE, 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.



Return val.

Number of characters output



if successful.                                                                                              


Integer< 0

if an error occurs..

Notes

printf rounds to the specified precision when converting floating-point numbers.

printf does not convert one data type to another. A value must be explicitly converted (e.g. with the cast operator) if it is not to be output in accordance with its type.

The data is not written immediately to the external file but is stored in an internal C buffer (see section “Buffering” (Basic terms)).

Maximum number of characters to be output:
With KR functionality (applies to C/C++ versions prior to V3.0 only) a maximum of 1400 characters can be output per printf call, with ANSI functionality a maximum of 1400 characters per conversion element (e.g. %s).

Attempts to output non-initialized variables or to output variables in a manner inconsistent with their data type can lead to undefined results.

The behavior is undefined if the percent sign (%) in a format statement is followed by an undefined formatting or conversion character.

printf works like fprintf, except that the data is written to the standard output and not to a file.

The following applies in the case of text files with SAM access mode and variable record length for which a maximum record length is also specified: When the specification split=no was entered for fopen, records which are longer than the maximum record length are truncated to the maximum record length when they are written. By default or with the specification split=yes, these records are split into multiple records. If a record has precisely the maximum record length, a record of the length zero is written after it.

Example 1

Output of the date and time in the following form:

Thursday, February 14, 12:05 hours

The arguments weekday and month are pointers to strings terminated with ’\0’.

printf("%s, %s %d, %02d:%02d hours\n", weekday, day, month, hrs, min);

Example 2

Output of the number pi to 5 decimal places.

printf("pi = %.5f\n", 4 * atan(1.0));

Example 3

The most common printf formats are self-explanatory in the way they are used in the other program examples. In the following table, you will find some additional format specifications listed along with their effects.
For clarity, the converted result is placed in > <.

Format specification

Argument()

Result

%.6s

"Konstanz"

>Konsta<

%10.5s

"Konstanz"

> Konst<

%-10.5s

"Konstanz"

>Konst <

%15.15s

"Konstanz"

> Konstanz<

%*.*s

20,7,"Konstanz"

> Konstan<

%-*.*s

15,10,"Konstanz"

>Konstanz <

%8d

721932

> 721932<

%-8d

721932

>721932 <

%-*.*f

3,2,27.31928

>27.32<

%-0*.*f

1,12,19.84

>19.840000000000<

%04.*f

12,10.60

>10.600000000000<

%-0*.*g

1,12,19.84

>19.84<

%e

1712.1961

>1.712196e+03<

%.10e

1712.1961

>1.7121961000e+03<

%10.10e

1712.1961

>1.7121961000e+03<

See also

fprintf, sprintf, snprintf, putc, putchar, puts, scanf, fscanf