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 printf function places output on the standard output stream, stdout. The fprintf subroutine places output on the named output stream. The sprintf 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, formats, 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, printf pads the value with blanks. By default, printf pads the value on the left. If the conversion string specifies the value is left-justified, printf pads the value on the right. If the field width begins with a zero, printf 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 character, 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 supplies 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, printf pads the value on the left with a blank. If the blank and plus sign (+) flags both appear, printf ignores the blank flag.
#The result has been converted to a different format. 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, printf 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, printf does not remove trailing zeros from the result.
The conversion characters and their meanings are as follows:
doxConvert the integer argument to decimal, octal, or hexadecimal notation, respectively.
fConvert the floating point or double precision argument to decimal notation in the style [− ]ddd.ddd, where the number of ds following the radix character is equal to the precision for the argument. If the precision is missing, printf prints six digits. If the precision is explicitly zero, the function prints no digits and no radix characters.
eConvert 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, printf prints six digits.
gConvert the floating point or double precision argument to style d, style f, or style e. The style printf 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 followed by a digit, printf 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 printf function uses style f.
cPrint the character argument.
sPrint the character argument. The printf 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, printf prints the character argument until it encounters a null character.
uConvert 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.
iConvert the integer argument to decimal. (This conversion character is the same as d.)
nStore the number of characters formatted in the integer argument.
pPrint 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 printf uses is defined by the last successful call to setlocale category LC_NUMERIC. If setlocale category LC_NUMERIC has not been called successfully or if the radix character is undefined, the radix character defaults to a period (.).
International Environment
LC_NUMERICIf this environment is set and valid, printf uses the international language database named in the definition to determine radix character rules.
LANGIf this environment variable is set and valid printf uses the international language database named in the definition to determine collation and character classification rules. If LC_NUMERIC is defined, its definition supercedes the definition of LANG.
Restrictions
The printf function cannot format values that exceed 128 characters.
Examples
To print a date and time in the form Sunday, July 3, 10:02, where weekday 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 π to 5 decimal places use the following call:
printf("pi = %.5f", 4*atan(1.0));
Return Values
In the BSD environment, printf and fprintf return zero for success and EOF for failure. The sprintf subroutine returns its first argument for success and EOF for failure.
In the System V and POSIX environments, printf, fprintf, and sprintf return the number of characters transmitted for success. The sprintf function ignores the null terminator (\0) when calculating the number of characters transmitted. If an output error occurs, these routines return a negative value.
See Also
ecvt(3), nl_printf(3int), nl_scanf(3int), setlocale(3), putc(3s), scanf(3s), environ(5int)
Guide to Developing International Software