sprintf

Performs formatted output to a string in memory.

Format

#include  <stdio.h>

int sprintf  (char *str, const char
             *format_spec, . . . );

Arguments

str
The address of the string that will receive the formatted output. It is assumed that this string is large enough to hold the output.
format_spec
A pointer to a character string that contains the format specification. For more information about format specifications and conversion characters, see Chapter 2.
. . .
Optional expressions whose resultant types correspond to conversion specifications given in the format specification.

If no conversion specifications are given, you may omit the output sources. Otherwise, the function calls must have at least as many output sources as there are conversion specifications, and the conversion specifications must match the types of the output sources.

Conversion specifications are matched to output sources in left-to- right order. Excess output pointers, if any, are ignored.

Description

A null character is automatically appended to the end of the output string. Consider the following example of a conversion specification:
#include <stdio.h>

main()
{
   int  temp = 4, temp2 = 17;
   char s[80];

   sprintf(s, "The answers are %d, and %d.", temp, temp2);
}

In this example, character string s has the following contents:

The answers are 4, and 17.

For a complete description of the format specification and the output source, see Chapter 2.

Return Value
The number of characters placed in the output string, not including the final null character. 
Negative value  Indicates an output error occurred. The function sets errno. For a list of errno values set by this function, see fprintf in this section. 


Previous Page | Next Page | Table of Contents | Index