HP C/iX Library Reference Manual (30026-90004)
Chapter 5 317
HP C/iX Library Function Descriptions
sprintf
sprintf
Writes formatted data to a character string in memory.
Syntax
#include <stdio.h>
int sprintf (char *
string
, const char *
format
[,item [,item]...]
);
Parameters
string
A pointer to a buffer in memory where the data is to be written.
format
Pointer to a character string defining the format (or the character string
itself enclosed in double quotes).
item
,… Each
item
is a variable or expression specifying the data to write. Refer
below to descriptions of conversion specifications and characters.
Return Values
≥0 If successful, the number of characters written, not counting the
terminating null character.
<0 An error occurred.
Description
The sprintf function enables you to write data to a buffer in formatted form. The
string
parameter is a buffer in memory where the data is written. The
format
parameter is a
pointer to a character string (or the character string itself enclosed in double quotes) which
specifies the format and content of the data to be written. Each
item
is a variable or
expression specifying the data to write.
The only difference between sprintf() and printf() is that sprintf() writes data into
a character array, while printf() writes data to stdout, the standard output device.
The sprintf() format is made up of conversion specifications and literal characters.
Literal characters are all characters that are not part of a conversion specification. Literal
characters are written to
string
exactly as they appear in the format.
The sprintf function returns the number of characters written or a negative value (if an
error is returned).
Conversion Specifications
The following list shows the different components of a conversion specification in their
correct sequence:
1. A percent sign (%), which signals the beginning of a conversion specification; to output
a literal percent sign, you must type two percent signs (%%).