sprintf()

Syntax
#include <stdio.h>

int sprintf(char *s, const char *format, ...);

Description

sprintf() writes formatted output to string s. It evaluates the arguments, converts them according to format, and writes the result to s, terminated with a zero character.

The format string contains the text to be printed. Any character sequence in format starting with % is a format specifier that is replaced by the corresponding argument. The first format specifier is replaced with the first argument after format, the second format specifier by the second argument, and so on.

A format specifier has the form:

FormatSpec      = % {Format}[Width][Precision] 
                    [Length]Conversion 

where:

Format          = -|+|<a blank>|#|0

The format defines justification and sign information (the latter only for numerical arguments). A - left justifies the output, a + forces output of the sign, and a blank outputs a blank if the number is positive and a - if it is negative. The following table describes the effect of # depending on the conversion character.

Table 1. Effect of # in the Format Specification
Conversion Effect of #
e, E, f Always prints the value of the argument with decimal point, even if there are no fractional digits.
g, G As above, but also appends zeroes to the fraction until the specified width is reached.
o Prints a zero before the number to indicate an octal value.
x, X Prints 0x (if the conversion is x) or 0X (if it is X) before the number to indicate a hexadecimal value.
Others Undefined

A 0 as format specifier adds leading zeroes to the number until the desired width is reached, if the conversion character specifies a numerical argument.

If both " " (blank) and + are given, only + is active; if both 0 and - are specified, only - is active. If there is a precision specification for integral conversions, 0 is ignored.

If the precision specifier is *, the precision is taken from the next argument, which must be an int. If that value is negative, the precision is ignored.

These conversion characters have the following meanings:

Table 3. Meaning of the Conversion Characters
Conversion Description
c Converts the int argument to unsigned char; prints the resulting character
d, i Prints an int argument
e, E Argument must be a double. Prints in the form [-]d.ddde±dd (scientific notation). The precision determines the number of fractional digits, the digit to the left of the decimal is ¦ 0 unless the argument is 0.0. The default precision is 6 digits. If the precision is zero and the format specifier # is not given, no decimal point prints. The exponent always has at least two digits; the conversion character prints just before the exponent.
f Argument must be a double. Prints in the form [-]ddd.ddd. See above. If the decimal point prints, there is at least one digit to the left of it.
g, G Argument must be a double. sprintf() chooses either format f or e (or E if G is given), depending on the magnitude of the value. Uses scientific notation only if the exponent is < -4 or greater than or equal to the precision.
n Argument must be a pointer to an int. sprintf() writes the number of characters written so far to that address. If n is used together with length specifier h or l, the argument must be a pointer to a short int or a long int.
o Prints the argument, which must be an unsigned int, in octal notation.
p Argument must be a pointer; prints its value in hexadecimal notation.
s Argument must be a char *; sprintf() writes the string.
u Writes the argument, which must be an unsigned int, in decimal notation.
x, X Writes the argument, which must be an unsigned int, in hexadecimal notation. x uses lower case letters a to f, while X uses upper case letters.
% Prints a % sign. Give as %% only.

Conversion characters for integral types are d, i, o, u, x, and X; for floating point types e, E, f, g, and G.

If sprintf() finds an incorrect format specification, it stops processing, terminates the string with a zero character, and returns successfully.

Note: Floating point support increases the sprintf size considerably, and therefore the define LIBDEF_PRINTF_FLOATING exists. Set LIBDEF_PRINTF_FLOATING if no floating point support is used. Some targets contain special libraries without floating point support.
Return

The number of characters written to s.

See also

sscanf()