printf.3s (2010 09)

p
printf(3S) printf(3S)
NAME
printf(), fprintf(), sprintf(), snprintf() - print formatted output
SYNOPSIS
#include <stdio.h>
int printf(const char *__restrict format, /* [arg,] */ ...);
int fprintf(FILE *__restrict stream, const char *__restrict format,
/* [arg,] */ ...);
int sprintf(char *__restrict s, const char *__restrict format,
/* [arg,] */ ...);
int snprintf(char *__restrict s, size_t maxsize,
const char *__restrict format, /* [arg,] */ ...);
DESCRIPTION
printf() places output on the standard output stream stdout .
fprintf() places output on the named output stream .
sprintf() places "output", followed by the null character (
\0), in consecutive bytes starting at *
s. It is
the user’s responsibility to ensure that enough storage is available.
snprintf() behaves like sprintf(), except that it limits the number of characters written to the
destination buffer to maxsize, including the terminating null character.
Each function converts, formats, and prints its arg s under control of the format . format is a character
string containing two types of objects: plain characters that are copied to the output stream, and conver-
sion specifications, each of which results in fetching zero or more args. The results are undefined if there
are insufficient args for the format. If the format is exhausted while arg s remain, excess args are
ignored.
Each conversion specification is introduced by the character
% or %n$, where n is a decimal integer in the
range 1 through
{NL_ARGMAX} (NL_ARGMAX is defined in <limits.h>). The %n
$ construction indi-
cates that this conversion should be applied to the nth argument, rather than to the next unused one.
An argument can be referenced by a
%n$ specification more than once. The two forms of introducing a
conversion specification, % and %n$, cannot be mixed within a single format string. When numbered
argument specifications are used, specifying the Nth argument requires that all the leading arguments,
from the first to the (N-1)th, are specified in the format string. The results of mixing numbered and
unnumbered argument specifications in a format string are undefined.
After the
% or %n$, the following appear in sequence:
1. Zero or more flags, which modify the meaning of the conversion specification.
2. An optional string of decimal digits to specify a minimum field width in bytes. If the converted
value has fewer characters than the field width, it is be padded on the left (or right, if the left-
adjustment flag (
-), described below, has been given) to the field width. If the field width is
preceded by a zero, the string is right adjusted with zero-padding on the left (see the leading-
zero flag, 0, described below).
3. A precision that gives the minimum number of digits to appear for the
d, i, o, u, x,orX
conversions, the number of digits to appear after the radix character for the a, e, and f
conversions, the maximum number of significant digits for the g conversion, or the maximum
number of bytes to be printed from a string in the s conversion. The precision takes the form
of a period followed by a decimal digit string; a null digit string is treated as zero.
4. Zero or more of the following optional character specifications:
an optional
h, specifying that a following d, i, o, u, x,orX conversion character applies
to a short integer arg
an optional
h specifying that a following n conversion character applies to a pointer to a
short integer arg
an optional
hh specifying that a following d, i, o, u, x or X conversion specifier applies to
a signed char or unsigned char argument (the argument will have been promoted accord-
ing to the integer promotions, but its value will be converted to short or unsigned short
before printing)
HP-UX 11i Version 3: September 2010 1 Hewlett-Packard Company 1

Summary of content (6 pages)