PRINTF(3) Linux Programmer's Manual PRINTF(3)
NAME
printf(1,3,1 builtins), fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
vsnprintf - formatted output conversion
SYNOPSIS
#include <stdio.h>
int printf(1,3,1 builtins)(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
#include <stdarg.h>
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
DESCRIPTION
The functions in(1,8) the printf(1,3,1 builtins) family produce output according to a format
as described below. The functions printf(1,3,1 builtins) and vprintf write(1,2) output to
stdout, the standard output stream; fprintf and vfprintf write(1,2) output
to the given output stream; sprintf, snprintf, vsprintf and vsnprintf
write(1,2) to the character string(3,n) str.
The functions vprintf, vfprintf, vsprintf, vsnprintf are equivalent to
the functions printf(1,3,1 builtins), fprintf, sprintf, snprintf, respectively, except
that they are called with a va_list instead of a variable number of
arguments. These functions do not call the va_end macro. Consequently,
the value of ap is undefined after the call. The application should
call va_end(ap) itself afterwards.
These eight functions write(1,2) the output under the control of a format
string(3,n) that specifies how subsequent arguments (or arguments accessed
via the variable-length argument facilities of stdarg(3)) are converted
for output.
Return value
Upon successful return, these functions return the number of characters
printed (not including the trailing '\0' used to end output to
strings). The functions snprintf and vsnprintf do not write(1,2) more than
size bytes (including the trailing '\0'). If the output was truncated
due to this limit then the return value is the number of characters
(not including the trailing '\0') which would have been written to the
final string(3,n) if(3,n) enough space had been available. Thus, a return value
of size or more means that the output was truncated. (See also below
under NOTES.) If an output error(8,n) is encountered, a negative value is
returned.
Format of the format string(3,n)
The format string(3,n) is a character string(3,n), beginning and ending in(1,8) its
initial shift state, if(3,n) any. The format string(3,n) is composed of zero or
more directives: ordinary characters (not %), which are copied
unchanged to the output stream; and conversion specifications, each of
which results in(1,8) fetching zero or more subsequent arguments. Each con-
version(1,3,5) specification is introduced by the character %, and ends with a
conversion specifier. In between there may be (in(1,8) this order) zero or
more flags, an optional minimum field width, an optional precision and
an optional length modifier.
The arguments must correspond properly (after type promotion) with the
conversion specifier. By default, the arguments are used in(1,8) the order
given, where each `*' and each conversion specifier asks for the next
argument (and it is an error(8,n) if(3,n) insufficiently many arguments are
given). One can also specify explicitly which argument is taken, at
each place where an argument is required, by writing `%m$' instead of
`%' and `*m$' instead of `*', where the decimal integer m denotes the
position in(1,8) the argument list of the desired argument, indexed starting
from 1. Thus,
printf(1,3,1 builtins)("%*d", width, num);
and
printf(1,3,1 builtins)("%2$*1$d", width, num);
are equivalent. The second style allows repeated references to the same
argument. The C99 standard does not include the style using `$', which
comes from the Single Unix Specification. If the style using `$' is
used, it must be used throughout for all conversions taking an argument
and all width and precision arguments, but it may be mixed with `%%'
formats which do not consume an argument. There may be no gaps in(1,8) the
numbers of arguments specified using `$'; for example, if(3,n) arguments 1
and 3 are specified, argument 2 must also be specified somewhere in(1,8) the
format string.
For some numeric conversions a radix character (`decimal point') or
thousands' grouping character is used. The actual character used
depends on the LC_NUMERIC part of the locale. The POSIX locale(3,5,7) uses `.'
as radix character, and does not have a grouping character. Thus,
printf(1,3,1 builtins)("%'.2f", 1234567.89);
results in(1,8) `1234567.89' in(1,8) the POSIX locale(3,5,7), in(1,8) `1234567,89' in(1,8) the
nl_NL locale(3,5,7), and in(1,8) `1.234.567,89' in(1,8) the da_DK locale.
The flag characters
The character % is followed by zero or more of the following flags:
# The value should be converted to an ``alternate form''. For o
conversions, the first character of the output string(3,n) is made
zero (by prefixing a 0 if(3,n) it was not zero already). For x and X
conversions, a non-zero result has the string(3,n) `0x' (or `0X' for
X conversions) prepended to it. For a, A, e, E, f, F, g, and G
conversions, the result will always contain a decimal point,
even if(3,n) no digits follow it (normally, a decimal point appears
in(1,8) the results of those conversions only if(3,n) a digit follows).
For g and G conversions, trailing zeros are not removed from the
result as they would otherwise be. For other conversions, the
result is undefined.
0 The value should be zero padded. For d, i, o, u, x, X, a, A, e,
E, f, F, g, and G conversions, the converted value is padded on
the left with zeros rather than blanks. If the 0 and - flags
both appear, the 0 flag is ignored. If a precision is given
with a numeric conversion (d, i, o, u, x, and X), the 0 flag is
ignored. For other conversions, the behavior is undefined.
- The converted value is to be left adjusted on the field bound-
ary. (The default is right justification.) Except for n conver-
sions, the converted value is padded on the right with blanks,
rather than on the left with blanks or zeros. A - overrides a 0
if(3,n) both are given.
' ' (a space) A blank should be left before a positive number (or
empty string(3,n)) produced by a signed conversion.
+ A sign (+ or -) always be placed before a number produced by a
signed conversion. By default a sign is used only for negative
numbers. A + overrides a space if(3,n) both are used.
The five flag characters above are defined in(1,8) the C standard. The
SUSv2 specifies one further flag character.
' For decimal conversion (i, d, u, f, F, g, G) the output is to be
grouped with thousands' grouping characters if(3,n) the locale(3,5,7) infor-
mation indicates any. Note that many versions of gcc cannot
parse this option and will issue a warning. SUSv2 does not
include %'F.
glibc 2.2 adds one further flag character.
I For decimal integer conversion (i, d, u) the output uses the
locale(3,5,7)'s alternative output digits, if(3,n) any. For example, since
glibc 2.2.3 this will give Arabic-Indic digits in(1,8) the Persian
(`fa_IR') locale.
The field width
An optional decimal digit string(3,n) (with nonzero first digit) specifying
a minimum field width. If the converted value has fewer characters
than the field width, it will be padded with spaces on the left (or
right, if(3,n) the left-adjustment flag has been given). Instead of a deci-
mal digit string(3,n) one may write(1,2) `*' or `*m$' (for some decimal integer
m) to specify that the field width is given in(1,8) the next argument, or in(1,8)
the m-th argument, respectively, which must be of type int. A negative
field width is taken as a `-' flag followed by a positive field width.
In no case does a non-existent or small field width cause truncation of
a field; if(3,n) the result of a conversion is wider than the field width,
the field is expanded to contain the conversion result.
The precision
An optional precision, in(1,8) the form of a period (`.') followed by an
optional decimal digit string. Instead of a decimal digit string(3,n) one
may write(1,2) `*' or `*m$' (for some decimal integer m) to specify that the
precision is given in(1,8) the next argument, or in(1,8) the m-th argument,
respectively, which must be of type int. If the precision is given as
just `.', or the precision is negative, the precision is taken to be
zero. This gives the minimum number of digits to appear for d, i, o,
u, x, and X conversions, the number of digits to appear after the radix
character for a, A, e, E, f, and F conversions, the maximum number of
significant digits for g and G conversions, or the maximum number of
characters to be printed from a string(3,n) for s and S conversions.
The length modifier
Here, `integer conversion' stands for d, i, o, u, x, or X conversion.
hh A following integer conversion corresponds to a signed char or
unsigned char argument, or a following n conversion corresponds
to a pointer to a signed char argument.
h A following integer conversion corresponds to a short int or
unsigned short int argument, or a following n conversion corre-
sponds to a pointer to a short int argument.
l (ell) A following integer conversion corresponds to a long int
or unsigned long int argument, or a following n conversion cor-
responds to a pointer to a long int argument, or a following c
conversion corresponds to a wint_t argument, or a following s
conversion corresponds to a pointer to wchar_t argument.
ll (ell-ell). A following integer conversion corresponds to a long
long int or unsigned long long int argument, or a following n
conversion corresponds to a pointer to a long long int argument.
L A following a, A, e, E, f, F, g, or G conversion corresponds to
a long double argument. (C99 allows %LF, but SUSv2 does not.)
q (`quad'. BSD 4.4 and Linux libc5 only. Don't use.) This is a
synonym for ll.
j A following integer conversion corresponds to an intmax_t or
uintmax_t argument.
z A following integer conversion corresponds to a size_t or
ssize_t argument. (Linux libc5 has Z with this meaning. Don't
use it.)
t A following integer conversion corresponds to a ptrdiff_t argu-
ment.
The SUSv2 only knows about the length modifiers h (in(1,8) hd, hi, ho, hx,
hX, hn) and l (in(1,8) ld(1,8), li, lo, lx, lX, ln, lc, ls) and L (in(1,8) Le, LE, Lf,
Lg, LG).
The conversion specifier
A character that specifies the type of conversion to be applied. The
conversion specifiers and their meanings are:
d,i The int argument is converted to signed decimal notation. The
precision, if(3,n) any, gives the minimum number of digits that must
appear; if(3,n) the converted value requires fewer digits, it is
padded on the left with zeros. The default precision is 1. When
0 is printed with an explicit precision 0, the output is empty.
o,u,x,X
The unsigned int argument is converted to unsigned octal (o),
unsigned decimal (u), or unsigned hexadecimal (x and X) nota-
tion. The letters abcdef are used for x conversions; the let-
ters ABCDEF are used for X conversions. The precision, if(3,n) any,
gives the minimum number of digits that must appear; if(3,n) the con-
verted value requires fewer digits, it is padded on the left
with zeros. The default precision is 1. When 0 is printed with
an explicit precision 0, the output is empty.
e,E The double argument is rounded and converted in(1,8) the style
[-]d.ddde±dd where there is one digit before the decimal-point
character and the number of digits after it is equal to the pre-
cision; if(3,n) the precision is missing, it is taken as 6; if(3,n) the
precision is zero, no decimal-point character appears. An E
conversion uses the letter E (rather than e) to introduce the
exponent. The exponent always contains at least two digits; if(3,n)
the value is zero, the exponent is 00.
f,F The double argument is rounded and converted to decimal notation
in(1,8) the style [-]ddd.ddd, where the number of digits after the
decimal-point character is equal to the precision specification.
If the precision is missing, it is taken as 6; if(3,n) the precision
is explicitly zero, no decimal-point character appears. If a
decimal point appears, at least one digit appears before it.
(The SUSv2 does not know about F and says that character string(3,n)
representations for infinity and NaN may be made available. The
C99 standard specifies `[-]inf' or `[-]infinity' for infinity,
and a string(3,n) starting with `nan' for NaN, in(1,8) the case of f con-
version(1,3,5), and `[-]INF' or `[-]INFINITY' or `NAN*' in(1,8) the case of
F conversion.)
g,G The double argument is converted in(1,8) style f or e (or F or E for
G conversions). The precision specifies the number of signifi-
cant digits. If the precision is missing, 6 digits are given;
if(3,n) the precision is zero, it is treated as 1. Style e is used
if(3,n) the exponent from its conversion is less(1,3) than -4 or greater
than or equal to the precision. Trailing zeros are removed from
the fractional part of the result; a decimal point appears only
if(3,n) it is followed by at least one digit.
a,A (C99; not in(1,8) SUSv2) For a conversion, the double argument is
converted to hexadecimal notation (using the letters abcdef) in(1,8)
the style [-]0xh.hhhhp±d; for A conversion the prefix 0X, the
letters ABCDEF, and the exponent separator P is used. There is
one hexadecimal digit before the decimal point, and the number
of digits after it is equal to the precision. The default pre-
cision suffices for an exact representation of the value if(3,n) an
exact representation in(1,8) base 2 exists and otherwise is suffi-
ciently large to distinguish values of type double. The digit
before the decimal point is unspecified for non-normalized num-
bers, and nonzero but otherwise unspecified for normalized num-
bers.
c If no l modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written. If an l
modifier is present, the wint_t (wide character) argument is
converted to a multibyte sequence by a call to the wcrtomb func-
tion, with a conversion state starting in(1,8) the initial state, and
the resulting multibyte string(3,n) is written.
s If no l modifier is present: The const char * argument is
expected to be a pointer to an array of character type (pointer
to a string(3,n)). Characters from the array are written up to (but
not including) a terminating NUL character; if(3,n) a precision is
specified, no more than the number specified are written. If a
precision is given, no null character need be present; if(3,n) the
precision is not specified, or is greater than the size of the
array, the array must contain a terminating NUL character.
If an l modifier is present: The const wchar_t * argument is
expected to be a pointer to an array of wide characters. Wide
characters from the array are converted to multibyte characters
(each by a call to the wcrtomb function, with a conversion state
starting in(1,8) the initial state before the first wide character),
up to and including a terminating null wide character. The
resulting multibyte characters are written up to (but not
including) the terminating null byte. If a precision is speci-
fied, no more bytes than the number specified are written, but