printf(3s)printf(3s)Name
printf, fprintf, sprintf - formatted output conversion
Syntax
#include <stdio.h>
int printf( format [, arg ] ... )
char *format;
int fprintf( stream, format [, arg ] ...
FILE *stream;
char *format;
BSD Environment
char *sprintf( s, format [, arg ] ... )
char *s, *format;
System V and POSIX Environments
int sprintf( s, format [, arg ] ... )
char *s, format;
Description
The function places output on the standard output stream, The subrou‐
tine places output on the named output stream. The subroutine places
output in the string s, and appends the null terminator `\0' to the end
of the string.
The first argument controls how each of these functions converts, for‐
mats, and prints the other arguments. The first argument is a character
string that contains two types of objects, characters and conversion
specifications. These functions copy characters that appear in the
first argument to the output stream. Conversion specifications cause
these functions to convert the next successive argument and send the
formatted argument to the output stream.
You introduce conversion specifications using the percent sign (%).
Following the %, you can include:
· Zero or more flags, which modify the meaning of the conversion
specification.
· An optional minus sign (-), which specifies left adjustment of the
converted value in the indicated field.
· An optional digit string that specifies a field width. If the
converted value has fewer characters than the field width, pads
the value with blanks. By default, pads the value on the left.
If the conversion string specifies the value is left-justified,
pads the value on the right. If the field width begins with a
zero, pads the values with zeros, instead of blanks.
· An optional period (.), which separates the field width from the
next digit string.
· An optional digit string specifying a precision. The precision
controls the number of digits that appear after the radix charac‐
ter, exponential and floating-point conversions. Precision also
controls the maximum number of characters that are placed in the
converted value for a string.
· The character h or l specifying that a following d, i, o, u, x, or
X corresponds to an integer or longword integer argument. You can
use an uppercase L or a lowercase l.
· A character that indicates the type of conversion to be applied.
A field width or precision can be an asterisk (*), instead of a digit
string. If you use an asterisk, you can include an argument that sup‐
plies the field width or precision.
The flag characters and their meanings are as follows:
- The result of the conversion is left-justified within the field.
+ The result of a signed conversion always begins with a sign (+ or
-).
blank
If the first character of a signed conversion is not a sign, pads
the value on the left with a blank. If the blank and plus sign
(+) flags both appear, ignores the blank flag.
The The value is to be converted to an alternative form.
For c, d, s, and u conversions, this flag has no effect.
For o
conversions, this flag increases the precision to force the first
digit of the result to be a zero.
For x or X conversions, pads a non-zero result on the left with 0x
or 0X.
For e, E, f, g, and G conversions, the result always contains a
radix character, even if no digits follow that character. (A
radix character usually appears in the result of these conversions
only if a digit follows it.)
For g and G conversions, does not remove trailing zeros from the
result.
The conversion characters and their meanings are as follows:
dox Convert the integer argument to decimal, octal, or hexadecimal
notation, respectively.
f Convert the floating point or double precision argument to decimal
notation in the style [- ]ddd.ddd, where the number of ds follow‐
ing the radix character is equal to the precision for the argu‐
ment. If the precision is missing, prints six digits. If the pre‐
cision is explicitly zero, the function prints no digits and no
radix characters.
e Convert the floating point or double precision argument in the
style [- ]d. ddde±dd, where one digit appears before the radix
character and the number of digits that appear after the radix
character is equal to the precision. When you omit the precision,
prints six digits.
g Convert the floating point or double precision argument to style
d, style f, or style e. The style uses depends on the format of
the converted value. The function removes trailing zeros before
evaluating the format of the converted value.
If a radix character appears in the converted value that is fol‐
lowed by a digit, uses style d. If the converted value contains
an exponent that is is less than -4 or greater than the precision,
the function uses style .BR e . Otherwise, the function uses
style f.
c Print the character argument.
s Print the character argument. The function prints the argument
until it encounters a null characters or has printed the number of
characters specified by the precision. If the precision is zero
or has not been specified, prints the character argument until it
encounters a null character.
u Convert the unsigned integer argument to a decimal value. The
result must be in the range of 0 through 4294967295, where the
upper bound is defined by MAXUNIT.
i Convert the integer argument to decimal. (This conversion charac‐
ter is the same as d.)
n Store the number of characters formatted in the integer argument.
p Print the pointer to the argument. (This conversion character is
the same as %08X).
% Print a percent sign ( % ). The function converts no argument.
A non-existent or small field width never causes truncation of a value.
Padding takes place only if the specified field width exceeds the
length of the value.
In all cases, the radix character uses is defined by the last success‐
ful call to category If category has not been called successfully or if
the radix character is undefined, the radix character defaults to a
period (.).
International Environment
LC_NUMERIC If this environment is set and valid, uses the interna‐
tional language database named in the definition to
determine radix character rules.
LANG If this environment variable is set and valid uses the
international language database named in the definition
to determine collation and character classification
rules. If is defined, its definition supercedes the
definition of LANG.
Restrictions
The function cannot format values that exceed 128 characters.
Examples
To print a date and time in the form Sunday, July 3, 10:02, where week‐
day and month are pointers to null-terminated strings use the following
function call:
printf("%s, %s %d, %02d:%02d",
weekday, month, day, hour, min);
To print pi to 5 decimal places use the following call:
printf("pi = %.5f", 4*atan(1.0));
Return Values
In the BSD environment, and return zero for success and EOF for fail‐
ure. The subroutine returns its first argument for success and EOF for
failure.
In the System V and POSIX environments, and return the number of char‐
acters transmitted for success. The function ignores the null termina‐
tor (\0) when calculating the number of characters transmitted. If an
output error occurs, these routines return a negative value.
See Alsoecvt(3), nl_printf(3int), nl_scanf(3int), setlocale(3), putc(3s),
scanf(3s), environ(5int)
Guide to Developing International Software
printf(3s)