printf function

int printf ( const char * format , ... ) ; <stdio.h>

Print formatted data to stdout.


The C printf function writes data to the standard output (stdout) formatted as specified in the format argument.


Parameters

format

string containing text to be written to stdout

additional

values for tags in format parameter

Depending on the format parameter, the function may expect a sequence of additional arguments, each containing one value to be inserted in place of its corresponding %-tag specified in the format argument.

If the format argument contains embedded tags, the tags are substituted with the values of subsequent arguments.

There should be the same number of arguments following the format argument as the number of tags that expect a value.

Tags are formatted as follows:

%[flags][width][.precision][length]specifier

where

% is the % character, identifying a tag.
flags consists of formatting flags, as follows:
- left-justifies within the given field width.
Right justification is the default.
+ forces the value to be preceeded with a plus or minus sign (+ or -) even for positive numbers.
By default, negative numbers are preceded with -, but positive numbers are not preceded with +.
(space) causes a blank space to be inserted before the value if no sign is written.
# formats numeric values as follows:
Used with o, x or X specifiers, the value is preceeded with 0, 0x or 0X respectively for values other than zero.
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.
0 left-pads the number with zeroes (0) instead of spaces, where padding is specified.
width specifies the minimum number of characters to be printed.
If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated if the result is larger.
If width is *, the width is not specified in the format string, but as an additional integer value argument preceding the argument to be formatted.
.precision specifies the precision to apply to the corresponding argument, as follows:
d, i, o, u, x and X specifiers the minimum number of digits to be written
If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated if the result is longer. A precision of 0 specifies that no character is written for the value 0.
e, E and f specifiers the number of digits to be printed after the decimal point
g and G specifiers the maximum number of significant digits to be printed
s specifier the maximum number of characters to be printed
By default all characters are printed until the ending null character is encountered.
c specifier no effect
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.
If .precision is .*, the precision is not specified in the format string, but as an additional integer value argument preceding the argument to be formatted.
length defines the length of the corresponding argument, as follows:
h The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).
l The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.
L The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).
specifier defines the type and the interpretation of the value of the corresponding argument, as follows:
c character
d or i signed decimal integer
e scientific notation using e character
E scientific notation using E character
f decimal floating point
g shorter of %e or %f
G shorter of %E or %f
o signed octal
s string of characters
u unsigned decimal integer
x unsigned hexadecimal integer
X unsigned hexadecimal integer using capital letters
p pointer address
n nothing printed.
The argument must be a pointer to a signed int, where the number of characters written so far is stored.
% %
A % character followed by another % character writes % to stdout.

Return Value

On success, the total number of characters written is returned.

On failure, a negative number is returned.


examples single parameter with no tags GCC C++
Borland C++ Compiler
multiple parameters and tags GCC C++
Borland C++ Compiler
home Home Page