C library functionsprintf()

C Standard Library - <stdio.h>

Description

C Library Functionsint sprintf(char *str, const char *format, ...)Sends formatted output tostrthe string pointed to.

Declaration

Below is the declaration of the sprintf() function.

int sprintf(char *str, const char *format, ...)

Parameter

  • str-- This is a pointer to an array of characters, which stores the C string.
  • format-- This is the string containing the text to be written to the string str. It can contain embedded format tags, which can be replaced by the values specified in subsequent additional arguments and formatted as required. The format tag attributes are: %[flags][width][.precision][length]specifier, with the details explained as follows:
specifier (specifier)Output
cCharacter
d or isigned decimal integer
eScientific notation using the e character (mantissa and exponent)
EScientific notation using the E character (mantissa and exponent)
fDecimal floating-point number
gAutomatically select the appropriate representation from %e or %f
GAutomatically select the appropriate representation from %E or %f
oSigned octal
sString of characters
uunsigned decimal integer
xunsigned hexadecimal integer
XUnsigned hexadecimal integer (uppercase letters)
pPointer address
nNo output
%Character

flagsDescription
-Left-align within the given field width; right-alignment is the default (see the width sub-specifier).
+Forces the result to be preceded by a plus or minus sign (+ or -), i.e., positive numbers will show a + sign. By default, only negative numbers are preceded by a - sign.
(space)If no sign is written, a space is inserted before the value.
#When used with the o, x, or X specifiers, non-zero values are preceded by 0, 0x, or 0X, respectively.
When used with e, E, and f, the output is forced to contain a decimal point, even if there are no digits after it. By default, if there are no digits after it, the decimal point is not displayed.
When used with g or G, the result is the same as with e or E, but trailing zeros are not removed.
0Place zeros (0) to the left of the number for the specified padding, rather than spaces (see the width sub-specifier).

widthDescription
(number)The minimum number of characters to be output. If the output value is shorter than this number, the result will be padded with spaces. If the output value is longer than this number, the result will not be truncated.
*The width is not specified in the format string, but is provided as an additional integer value argument placed before the argument to be formatted.

.precisionDescription
.numberFor integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the written value is shorter than this number, the result will be padded with leading zeros. If the written value is longer than this number, the result will not be truncated. A precision of 0 means no characters are written.
For e, E, and f specifiers: the number of digits to be output after the decimal point.
For g and G specifiers: the maximum number of significant digits to be output.
For s: the maximum number of characters to be output. By default, all characters are output until the terminating null character is encountered.
For c type: has no effect.
When no precision is specified, it defaults to 1. If specified without an explicit value, it is assumed to be 0.
.*The precision is not specified in the format string, but is provided as an additional integer value argument placed before the argument to be formatted.

lengthDescription
hThe argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x, and X).
lThe argument is interpreted as a long int or unsigned long int, applicable to integer specifiers (i, d, o, u, x, and X) and to the specifiers c (representing a wide character) and s (representing a wide character string).
LThe 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 series of additional arguments, each containing a value to be inserted, replacing each % tag specified in the format parameter. The number of arguments should be the same as the number of % tags.

Return Value

If successful, the total number of characters written is returned, excluding the null character appended to the end of the string. If it fails, a negative number is returned.

Example

The following example demonstrates the usage of the sprintf() function.

Example

#include <stdio.h>
#include <math.h>

int main()
{
   char str[80];

   sprintf(str, "The value of Pi = %f", M_PI);
   puts(str);
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

Pi 的值 = 3.141593

C Standard Library - <stdio.h>

other extensions