sprintf()

Syntax
  #include <stdio.h>

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

  
Description

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:

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.

The conversion characters have the following meanings, as the following table lists:

Table 3. Meaning of the Conversion Characters
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.

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. The IEEE64 floating point implementation is not supported.
Return

The number of characters written to s.

See also

sscanf()