printf(3S) printf(3S)
printf, fprintf, snprintf, sprintf - print formatted output
#include <stdio.h>
int printf(const char *format, .../* args */);
int fprintf(FILE *strm, const char *format, .../* args */);
int snprintf(char *s, size_t len, const char *format, .../* args */);
int sprintf(char *s, const char *format, .../* args */);
printf places output on the standard output stream stdout.
fprintf places output on strm.
sprintf places output, followed by a null character (\0), in consecutive
bytes starting at s. It is the user's responsibility to ensure that
enough storage is available.
snprintf places output, followed by a null character (\0), in consecutive
bytes starting at s. If more than len bytes of output would be
generated, the output is truncated at len bytes, including the trailing
null character.
Each function returns the number of characters transmitted (not including
the terminating null character in the case of snprintf and sprintf) or a
negative value if an output error was encountered.
Each of these functions converts, formats, and prints its args under
control of the format. The results are undefined if there are
insufficient arguments for the format. If the format is exhausted while
arguments remain, the excess arguments are simply ignored. The format is
a character string that contains two types of objects defined below:
1. plain characters that are simply copied to the output stream;
2. conversion specifications.
All forms of the printf functions allow for the insertion of a languagedependent
decimal-point character. The decimal-point character is
defined by the program's locale (category LC_NUMERIC). In the "C"
locale, or in a locale where the decimal-point character is not defined,
the decimal-point character defaults to a period (.).
Each conversion specification is introduced by the character %, and takes
the following general form and sequence:
%[posp<b>$][flags][width][.precision][size]fmt
Page 1
printf(3S) printf(3S)
posp<b>$ An optional entry, consisting of one or more decimal digits
followed by a $ character, specifying the number of the next arg to
access. The first arg (just after format) is numbered 1. If this
field is not specified, the arg following the most recently used
arg will be used.
flags Zero or more characters that modify the meaning of the conversion
specification. The flag characters and their meanings are:
' The integer portion of the result of a decimal conversion (for
b, B, i, d, u, f, g, or G conversions) will be formatted with
the thousands' grouping characters. The non-monetary grouping
character will be used.
- The result of the conversion will be left-justified within the
field. (It will be right-justified if this flag is not
specified.)
+ The result of a signed conversion will always begin with a
sign (+ or -). (It will begin with a sign only when a
negative value is converted if this flag is not specified.)
space
If the first character of a signed conversion is not a sign,
or if a signed conversion results in no characters, a space
will be prefixed to the result. If the space and + flags both
appear, the space flag will be ignored.
# The value is to be converted to an alternate form. For an o
conversion, it increases the precision (if necessary) to force
the first digit of the result to be a zero. For x (or X)
conversion, a nonzero result will have 0x (or 0X) prefixed to
it. For b, B, e, E, f, g, and G conversions, the result will
always contain a decimal-point character, even if no digits
follow it. (Normally, a decimal point 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). For c, d, i, s, and u
conversions, the flag has no effect.
0 For b, B, 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.
width An optional entry that consists of either one or more decimal
digits, or an asterisk (*), or an asterisk followed by one or more
decimal digits and a $. It specifies the minimum field width: If
the converted value has fewer characters than the field width, it
Page 2
printf(3S) printf(3S)
will be padded (with space by default) on the left or right (see
the above flags description) to the field width.
.prec An optional entry that consists of a period (.) followed by either
zero or more decimal digits, or an asterisk (*), or an asterisk
followed by one or more decimal digits and a $. It specifies 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 decimal-point
character for the b, B, e, E, and f conversions, the maximum number
of significant digits for the g and G conversions, or the maximum
number of characters to be written from a string for an s
conversion. For other conversions, the behavior is undefined. If
only a period is specified, the precision is taken as zero.
size An optional h, l (ell), ll (ell ell), or L that specifies other
than the default argument type of int for d and i; unsigned int for
o, u, x, and X; pointer to int for n; and double for b, B, e, E, f,
g, and G. If a size appears other than in the following
combinations, the behavior is undefined.
h For n, the argument has type pointer to short int; for d and
i, short int; and for o, u, x, and X, unsigned short int.
(For d, i, o, u, x, and X, the argument will have been
promoted according to the integral promotions, and its value
will be narrowed to short or unsigned short before printing.)
l For n, the argument has type pointer to long int; for d and i,
long int; and for o, u, x, and X, unsigned long int.
ll For n, the argument has type pointer to long long int; for d
and i, long long int; and for o, u, x, and X, unsigned long
long int.
L For b, B, e, E, f, g, and G, the argument has type long
double.
fmt A conversion character (described below) that indicates the type of
conversion to be applied.
When a width or .prec includes an asterisk (*), an int arg supplies the
width or precision. When they do not include a $, the arguments
specifying a field width, or precision, or both must appear (in that
order) before the argument (if any) to be converted. If the conversion
specification includes posp<b>$, the field width and precision may include a
$. The decimal digits that precede the $ similarly specify the number of
the arg that contains the field width or precision. (In this case, posp<b>$
specifies the number of the arg to convert.) A negative field width
argument is taken as a - flag followed by a positive field width. If the
precision argument is negative, it will be taken as zero.
Page 3
printf(3S) printf(3S)
When numbered argument specifications are used, specifying the Nth
argument requires that all the leading arguments, from the first to the
(N-1)th, be specified at least once, in a consistent manner, in the
format string.
The conversion characters and their meanings are:
b, B The floating arg is first converted to human readable byte
counts. Arg is repeatedly divided by 1024 (%b) or 1000 (%B)
until the value is less than 1000. The value is then printed
as with the %f format except that there is a one character
postfix of ' ', K, M, G, T, P, E, Z, or Y. The postfix is
lower case for %b and upper case for %B. The postfix tag
corresponds to bytes, kilobytes, megabytes, gigabytes, etc.
The definition of kilobyte is 2^10 for %b and 10^3 for %B, the
definition of megabyte is 2^20 for %b and 10^6 for %B, and so
on. The full table is:
%b Multiplier From %B Multiplier
---------------------------------------------
' ' 1 ' ' 1
k 2^10 (1024) kilo K 10^3 (1000)
m 2^20 mega M 10^6
g 2^30 giga G 10^9
t 2^40 tera T 10^12
p 2^50 peta P 10^15
e 2^60 exa E 10^18
z 2^70 zetta Z 10^21
y 2^80 yotta Y 10^24
The default precision is 3, i.e., 1024 printed with %b is
printed as if it were %.3fk and will yield 1.000k.
d, i The integer arg is converted to signed decimal. 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 a zero value with a precision of zero is
no characters.
o, u, x, X
The unsigned integer arg is converted to unsigned octal (o),
unsigned decimal (u), or unsigned hexadecimal notation (x and
X). The x conversion uses the letters abcdef and the X
conversion uses the letters ABCDEF. 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 a zero value with a precision of zero is
no characters.
Page 4
printf(3S) printf(3S)
f The floating arg is converted to decimal notation in the style
[-]ddd<b>.ddd, where the number of digits after the decimal-point
character [see setlocale(3C)] is equal to the precision
specification. If the precision is missing, it is taken as 6;
if the precision is zero and the # flag is not specified, no
decimal-point character appears. If a decimal-point character
appears, at least one digit appears before it. The value is
rounded to the appropriate number of digits.
e, E The floating arg is converted to the style [-]d<b>.ddd<b>e_dd, where
there is one digit before the decimal-point character (which is
nonzero if the argument is nonzero) 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 zero and the #
flag is not specified, no decimal-point 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 zero, the exponent is zero.
g, G The floating arg is converted in style f or e (or in style E in
the case of a G conversion character), with the precision
specifying the number of significant digits. If the precision
is zero, it is taken as one. The style used depends on the
value converted; style e (or E) will be used only if the
exponent resulting from the conversion is less than -4 or
greater than or equal to the precision. Trailing zeros are
removed from the fractional part of the result; a decimal-point
character appears only if it is followed by a digit.
c The integer arg is converted to an unsigned char, and the
resulting character is written.
C arg is interpreted as a wchar_t, converted to a multi-byte
sequence, and the resulting byte(s) are written.
s The arg is taken to be a pointer to an array of characters.
Characters from the array are written up to (but not including)
a terminating null character; if a precision is specified, no
more than that many characters are written. If a precision is
not specified or is greater than the size of the array, the
array must contain a terminating null character. (A null
pointer for arg will yield undefined results.)
S The arg is taken to be a pointer to an array of wide characters
(wchar_t). Each character from the array is converted to a
multi-byte sequence and the resulting byte(s) are written.
Conversion stops when there is a null wide character in the
array. If a precision is specified, no more than that many
characters are written. If a precision is not specified or is
greater than the size of the array, the array must contain a
terminating null character. (A null pointer for arg will yield
Page 5
printf(3S) printf(3S)
undefined results.)
p The arg is taken to be a pointer to void. The value of the
pointer is converted to an implementation-defined sequence of
printable characters, which matches those read by the %p
conversion of the scanf function.
n The arg is taken to be a pointer to an integer into which is
written the number of characters written so far by this call to
printf, fprintf, snprintf, or sprintf. No argument is
converted.
% Print a %; no argument is converted. The complete
specification must be simply %%.
If the form of the conversion specification does not match any of the
above, the results of the conversion are undefined. Similarly, the
results are undefined if there are insufficient args for the format. If
the format is exhausted while args remain, the excess args are ignored.
If a floating-point value is the internal representation for infinity,
the output is [_]inf, where inf is either inf or INF, depending on
whether the conversion character is lowercase or uppercase. Printing of
the sign follows the rules described above.
If a floating-point value is the internal representation for ``not-anumber,''
the output is [_]nan<b>0xm. Depending on the conversion
character, nan is either nan or NAN. Additionally, 0xm represents the
most significant part of the mantissa. Again depending on the conversion
character, x will be x or X, and m will use the letters abcdef or ABCDEF.
Printing of the sign follows the rules described above.
In no case does a nonexistent or small field width cause truncation of a
field; if the result of a conversion is wider than the field width, the
field is expanded to contain the conversion result. Characters generated
by printf and fprintf are printed as if the putc routine had been called
repeatedly.
To print a date and time in the form ``Sunday, July 3, 10:02,'' where
weekday and month are pointers to null-terminated strings:
printf("%s, %s %i, %d:%.2d",
weekday, month, day, hour, min);
To print pi to 5 decimal places:
printf("pi = %.5f", 4 * atan(1.0));
The following two calls to printf both produce the same result of
10 10 00300 10:
Page 6
printf(3S) printf(3S)
printf("%d %1$d %.*d %1$d", 10, 5, 300);
printf("%d %1$d %3$.*2$d %1$d", 10, 5, 300);
exit(2), lseek(2), write(2), abort(3C), ecvt(3C), setlocale(3C),
putc(3S), scanf(3S), stdio(3S).
printf, fprintf, snprintf, and sprintf return the number of characters
transmitted (not counting the terminating null character for snprintf and
sprintf), or return a negative value if an error was encountered.
PPPPaaaaggggeeee 7777 [ Back ]
|