#include <stdio.h>
int sprintf(char *s, const char *format, ...);
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.
| 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.
Number defines the minimum field width into which the output is to be put. If the argument is smaller, the space is filled as defined by the format characters.
0Number is the same as above, but uses zeroes instead of blanks.
If a * is given, the field width is taken from the next argument, which must be a number. If that number is negative, the output is left-justified.
The effect of the precision specification depends on the conversion character.
| Conversion | Precision |
|---|---|
| d, i, o, u, x, X | The minimum number of digits to print. |
| e, E, f | The number of fractional digits to print. |
| g, G | The maximum number of significant digits to print. |
| s | The maximum number of characters to print. |
| Others | Undefined. |
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.
A length specifier tells sprintf() what type the argument has. The first two length specifiers can be used in connection with all conversion characters for integral numbers. h defines short; l defines long. Use specifier L in conjunction with the conversion characters for floating point numbers to specify long double.
These conversion characters have the following meanings:
| 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.
The number of characters written to s.