HP-UX Reference (11i v2 03/08) - 3 Library Functions N-Z (vol 7)
p
printf(3S) printf(3S)
NAME
printf(), fprintf(), sprintf(), snprintf() - print formatted output
SYNOPSIS
#include <stdio.h>
int printf(const char *format, /* [arg,] */ ...);
int fprintf(FILE *stream, const char *format, /* [arg,] */ ...);
int sprintf(char *s, const char *format, /* [arg,] */ ...);
int snprintf(char *s, size_t maxsize, const char *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 args 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 args 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. An optional
l (the letter ‘‘ell’’), specifying that a following d, i, o, u, x,orX conversion charac-
ter applies to a long integer arg; an optional l specifying that a following n conversion charac-
ter applies to a pointer to a long integer arg; an optional h, specifying that a following d, i, o,
u, x,orX conversion character applies to a short integer arg; an optional hL specifying that a
following a, e, f,org conversion wide-character applies to a type extended which is the 80-
bit IEEE-754 double-extended type in the Itanium architecture; an optional ll, specifying that
a following d, i, o, u, x,orX conversion character applies to a long long integer arg; an
optional h specifying that a following n conversion character applies to a pointer to a short
integer arg; an optional L specifying that a following a, A, e, E, f, g,orG conversion charac-
ter applies to a long double arg.Anl, h, ll or L before any other conversion character is
ignored.
HP-UX 11i Version 2: August 2003 − 1 − Hewlett-Packard Company Section 3−−719