C Library Function - sprintf() | Rookie Tutorial
C Standard Library - <stdio.h>
Description
The C library function int sprintf(char *str, const char *format, ...) sends formatted output to the string pointed to by str.
Declaration
Below is the declaration of the sprintf() function.
int sprintf(char *str, const char *format, ...)
Parameters
- str -- This is a pointer to a character array that stores the C string.
- format -- This is a string containing the text to be written to string str. It may contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as required. The format tag attributes are %[.precision]specifier, explained in detail below:
| Specifier | Output |
|---|---|
| c | Character |
| d or i | Signed decimal integer |
| e | Scientific notation using e character (mantissa/exponent) |
| E | Scientific notation using E character (mantissa/exponent) |
| f | Decimal floating point |
| g | Automatically selects the more appropriate representation between %e or %f |
| G | Automatically selects the more appropriate representation between %E or %f |
| o | Signed octal |
| s | String of characters |
| u | Unsigned decimal integer |
| x | Unsigned hexadecimal integer |
| X | Unsigned hexadecimal integer (uppercase letters) |
| p | Pointer address |
| n | No output |
| % | % character |
| Flag | Description |
|---|---|
| - | Left-align within the given field width (right-aligned by default, see width sub-specifier). |
| + | Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign. |
| (space) | Inserts a space before the value if no sign is written. |
| # | When used with o, x or X specifiers, non-zero values are preceded with 0, 0x or 0X respectively. When used with e, E and f, forces the output to include a decimal point even when no digits follow. By default, no decimal point is written if no digits follow. When used with g or G, the result is the same as with e or E but trailing zeros are not removed. |
| 0 | Left-pads the number with zeros (0) instead of spaces when padding is specified (see width sub-specifier). |
| Width | Description |
|---|---|
| (number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. |
| * | The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
| Precision | Description |
|---|---|
| .number | For integer specifiers (d, i, o, u, x, X): specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E and f specifiers: the number of digits to be printed after the decimal point. For g and G specifiers: the maximum number of significant digits to be printed. For s: the maximum number of characters to be printed. By default, all characters are printed until the terminating null character is encountered. For c type: has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed. |
| .* | The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. |
| Length | Description |
|---|---|
| h | The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X). |
| l | The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s. |
| L | The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G). |
- Additional arguments -- Depending on the format string, the function may require a sequence of additional arguments, each containing a value to be inserted instead of each %-tag specified in the format parameter. The number of arguments should match the number of %-tags.
Return Value
On success, returns the total number of characters written, excluding the null-character appended at the end of the string. On failure, returns a negative number.
Example
The following example demonstrates the usage of the sprintf() function.
Example
#include <stdio.h>
#include <math.h>
int main()
{
char str;
sprintf(str,"Pi value = %f", M_PI);
puts(str);
return(0);
}
Let's compile and run the above program, which will produce the following result:
Pi value = 3.141593
YouTip