#include <stdio.h>
int sprintf(char *s, const char *format,...);
sprintf() writes formatted output to the s string. It evaluates the arguments, converts them according to the specified 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 a 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 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 effect of "#" depends on the Conversion character, as listed in the following chapter.
| Conversion | Effect of "#" |
|---|---|
| e, E, f | The value of the argument always is printed with decimal point, even if there are no fractional digits. |
| g, G | As above, but In addition zeroes are appended to the fraction until the specified width is reached. |
| o | A zero is printed before the number to indicate an octal value. |
| x, X | "0x" (if the conversion is "x") or "0X" (if it is "X") is printed 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 " " 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 0s are used instead of blanks.
If an asterisk "*" is given, the field width is taken from the next argument, which of course 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, as listed in the following table.
| 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. Specifier "L" is used in conjunction with the conversion characters for floating point numbers and specifies long double.
Conversion = c|d|e|E|f|g|
G|i|n|o|p|s|
u|x|X|%
The conversion characters have the following meanings, as the following table lists:
| Conversion | Description |
|---|---|
| c | The int argument is converted to unsigned char; the resulting character is printed. |
| d, i | An int argument is printed. |
| e, E | The argument must be a double. It is printed 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 is printed. The exponent always has at least 2 digits; the conversion character is printed just before the exponent. |
| f | The argument must be a double. It is printed in the form [-]ddd.ddd (see above). If the decimal point is printed, there is at least one digit to the left of it. |
| g, G | The 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. Scientific notation is used only if the exponent is < -4 or greater than or equal to the precision. |
| n | The 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 longint. |
| o | The argument, which must be an unsigned int, is printed in octal notation. |
| p | The argument must be a pointer; its value is printed in hexadecimal notation. |
| s | The argument must be a char *; sprintf() writes the string. |
| u | The argument, which must be an unsigned int, is written in decimal notation. |
| x, X | The argument, which must be an unsigned int, is written in hexadecimal notation. x uses lower case letters a to f, while X uses upper case letters. |
| % | Prints a " %" sign. Give only as " %%". |
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.