sprintf(3S) (BSD Compatibility Package) sprintf(3S)
NAME
sprintf, vsprintf - formatted output conversion
SYNOPSIS
/usr/ucb/cc [flag ...] file ... -lucb
#include <stdio.h>
char sprintf(char *s, *format [,arg] ...);
char *vsprintf(char *s, *format, valist ap);
DESCRIPTION
sprintf places output, followed by the NULL character (\0), in con-
secutive bytes starting at s; it is the user's responsibility to
ensure that enough storage is available.
vsprintf is the same as sprintf, except that instead of being called
with a variable number of arguments, it is called with an argument
list as defined by varargs.
Each of these functions converts, formats, and prints its arguments
under control of the format. The format is a character string which
contains two types of objects: plain characters, which are simply
copied to the output stream, and conversion specifications, each of
which causes conversion and printing of zero or more arguments. The
results are undefined if there are insufficient arguments for the for-
mat. If the format is exhausted while arguments remain, the excess
arguments are simply ignored.
Each conversion specification is introduced by the character %. After
the %, the following appear in sequence:
- Zero or more flags, which modify the meaning of the conversion
specification.
- An optional decimal digit string specifying a minimum "field
width". If the converted value has fewer characters than the field
width, it will be padded on the left (or right, if the left-
adjustment flag -, described below, has been given) to the field
width. The padding is with blanks unless the field width digit
string starts with a zero, in which case the padding is with zeros.
- A precision that gives the minimum number of digits to appear for
the d, i, o, u, x, or X conversions, the number of digits to appear
after the decimal point for the e, E, and f conversions, the max-
imum number of significant digits for the g and G conversion, or
the maximum number of characters to be printed from a string in s
conversion. The precision takes the form of a period "." followed
by a decimal digit string; a NULL digit string is treated as zero.
Padding specified by the precision overrides the padding specified
by the field width.
Page 1 Reliant UNIX 5.44 Printed 11/98
sprintf(3S) (BSD Compatibility Package) sprintf(3S)
- An optional l (ell) specifying that a following d, i, o, u, x, or X
conversion character applies to a long integer argument. An l
before any other conversion character is ignored.
- A character that indicates the type of conversion to be applied.
A field width or precision or both may be indicated by an asterisk (*)
instead of a digit string. In this case, an integer argument supplies
the field width or precision. The argument that is actually converted
is not fetched until the conversion letter is seen, so the arguments
specifying field width or precision must appear before the argument
(if any) to be converted. 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 changed to zero.
The flag characters and their meanings are:
- The result of the conversion will be left-justified within the
field.
+ The result of a signed conversion will always begin with a sign
(+ or -).
blank If the first character of a signed conversion is not a sign, a
blank will be prefixed to the result. This implies that if the
blank and + flags both appear, the blank flag will be ignored.
# This flag specifies that the value is to be converted to an
alternate form. For c, d, i, s, and u conversions, the flag has
no effect. For o conversion, it increases the precision to
force the first digit of the result to be a zero. For x or X
conversion, a non-zero result will have 0x or 0X prefixed to
it. For e, E, f, g, and G conversions, the result will always
contain a decimal point, even if no digits follow the point
(normally, a decimal point appears in the result of these
conversions only if a digit follows it). For g and G conver-
sions, trailing zeroes will not be removed from the result
(which they normally are).
The conversion characters and their meanings are:
d, i, o, u, x, X
The integer argument is converted to signed decimal (d or i),
unsigned octal (o), unsigned decimal (u), or unsigned hexade-
cimal notation (x and X), respectively; the letters abcdef are
used for x conversion and the letters ABCDEF for X conversion.
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 zeroes. (For compati-
bility with older versions, padding with leading zeroes may
alternatively be specified by prepending a zero to the field
width. This does not imply an octal value for the field width.)
Page 2 Reliant UNIX 5.44 Printed 11/98
sprintf(3S) (BSD Compatibility Package) sprintf(3S)
The default precision is 1. The result of converting a zero
value with a precision of zero is a NULL string.
f The float or double argument is converted to decimal notation
in the style [-]ddd.ddd where the number of digits after the
decimal point is equal to the precision specification. If the
precision is missing, 6 digits are given; if the precision is
explicitly 0, no digits and no decimal point are printed.
e, E The float or double argument is converted in the style
[-]d.ddde±ddd, where there is one digit before the decimal
point and the number of digits after it is equal to the preci-
sion; when the precision is missing, 6 digits are produced; if
the precision is zero, no decimal point appears. The E format
code will produce a number with E instead of e introducing the
exponent. The exponent always contains at least two digits.
g, G The float or double argument is printed in style f or e (or in
style E in the case of a G format code), with the precision
specifying the number of significant digits. 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 the precision. Trailing zeroes are removed from
the result; a decimal point appears only if it is followed by a
digit.
The e, E, f, g, and G formats print IEEE indeterminate values (infin-
ity or not-a-number) as Infinity or NaN respectively.
c The character argument is printed.
s The argument is taken to be a string (character pointer) and
characters from the string are printed until a NULL character
(\0) is encountered or until the number of characters indicated
by the precision specification is reached. If the precision is
missing, it is taken to be infinite, so all characters up to
the first NULL character are printed. A NULL value for argument
will yield undefined results.
% Print a %; no argument is converted.
In no case does a non-existent or small field width cause truncation
of a field; if the result of a conversion is wider than the field
width, the field is simply expanded to contain the conversion result.
Padding takes place only if the specified field width exceeds the
actual width.
RETURN VALUE
Upon success, sprintf and vsprintf always return s.
Page 3 Reliant UNIX 5.44 Printed 11/98
sprintf(3S) (BSD Compatibility Package) sprintf(3S)
EXAMPLES
The content of buf is a date and time in the form Sunday, July 3,
10:02, where weekday and month are pointers to NULL-terminated
strings:
sprintf(buf, "%s, %s %i, %d:%.2d", weekday, month, day, hour,
min);
The following routine pi has the effect that data is stored in buf
with a precision of five decimal places:
sprintf(buf, "pi = %.5f", 4 * atan(1. 0));
NOTES
Very wide fields (>128 characters) fail.
SEE ALSO
putc(3S), scanf(3S), vprintf(3S), varargs(5).
Page 4 Reliant UNIX 5.44 Printed 11/98