Go to the first, previous, next, last section, table of contents.
Syntax
#include <stdio.h>
int printf(const char *format, ...);
Description
Sends formatted output from the arguments (...) to stdout
.
The format string contains regular characters to print, as well as
conversion specifiers, which begin with a percent symbol. Each
conversion speficier contains the following fields:
-
an optional flag, which may alter the conversion:
-
-
left-justify the field.
+
-
Force a
+
sign on positive numbers.
space
-
To leave a blank space where a plus or minus sign would have been.
#
-
Alternate conversion - prefix octal numbers with
0
, hexadecimal
numbers with 0x
or 0X
, or force a trailing decimal point
if a floating point conversion would have omitted it.
0
-
To pad numbers with leading zeros.
-
A field width specifier, which specifies the minimum width of the field.
This may also be an asterisk (
*
), which means that the actual
width will be obtained from the next argument. If the argument is
negative, it supplies a -
flag and a positive width.
-
An optional decimal point and a precision. This may also be an
asterisk, but a negative argument for it indicates a precision of zero.
The precision specifies the minimum number of digits to print for an
integer, the number of fraction digits for a floating point number (max
for
g
or G
, actual for others), or the maximum number of
characters for a string.
-
An optional conversion qualifier, which may be
h
to specify
short
, l
to specify long ints, or L
to specify
long doubles. Long long type can be specified by L
or ll
.
-
The conversion type specifier:
c
-
A single character
d
-
A signed integer
D
-
A signed long integer
e
-
E
-
A floating point number (double or long double). The exponent case matches
the specifier case. The representation always has an exponent.
f
-
A floating point number (double or long double). The representation
never has an exponent.
g
-
G
-
A floating point number (double or long double). The exponent case matches
the specifier case. The representation has an exponent if it needs one.
i
-
A signed integer.
n
-
The next argument is a pointer to an integer, and the number of
characters generated so far is stored in that integer.
o
-
A unsigned integer, printed in base 8 instead of base 10.
p
-
A pointer. This is printed with an
x
specifier.
s
-
A
NULL
-terminated string.
u
-
An unsigned integer.
U
-
An unsigned long integer.
x
-
X
-
An unsigned integer, printed in base 16 instead of base 10. The case of
the letters used matches the specifier case.
%
-
A single percent symbol is printed.
Return Value
The number of characters written.
Portability
ANSI, POSIX
Example
printf("%-3d %10.2f%% Percent of %s\n", index, per[index], name[index]);
Go to the first, previous, next, last section, table of contents.