Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function – sprintf()

C Library Function - sprintf()

C Standard Library - <stdio.h> C Standard Library -

Description

C Library Function **int sprintf(char str, const char format, ...) Sends formatted output to str the string pointed to by .

Declaration

Below is the declaration of the sprintf() function.


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

Parameters

specifier (specifier) Output
c Character
d or i Signed decimal integer
e Scientific notation using the e character (mantissa and exponent)
E Scientific notation using the E character (mantissa and exponent)
f Decimal floating-point number
g Automatically select the appropriate representation between %e or %f
G Automatically select the 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
flags Description
- Left-justify within the given field width; right alignment is the default (see the width sub-specifier).
+ Force a plus or minus sign (+ or -) to appear before the result; that is, a + sign is shown before positive numbers. By default, only negative numbers are preceded by a - sign.
(space) If no sign is to be written, a space is inserted before the value.
# When used with o, x, or X specifiers, a nonzero value is prefixed with 0, 0x, or 0X, respectively.
 与 e、E 和 f 一起使用时,会强制输出包含一个小数点,即使后边没有数字时也会显示小数点。默认情况下,如果后边没有数字时候,不会显示显示小数点。       
 与 g 或 G 一起使用时,结果与使用 e 或 E 时相同,但是尾部的零不会被移除。 |

| 0 | Place zeros (0) to the left of the number for padding instead of spaces (see the width sub-specifier). |

width (width) Description
(number) The minimum number of characters to be output. If the value to be output is shorter than this number, the result is padded with spaces. If the value is longer than this number, the result is not truncated.
* The width is not specified in the format string but is placed as an additional integer argument before the argument to be formatted.
.precision Description
.number For integer specifiers (d, i, o, u, x, X): precision 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. If the value is longer than this number, the result is not truncated. A precision of 0 means no characters are written.
 对于 e、E 和 f 说明符:要在小数点后输出的小数位数。       
 对于 g 和 G 说明符:要输出的最大有效位数。       
 对于 s: 要输出的最大字符数。默认情况下,所有字符都会被输出,直到遇到末尾的空字符。       
 对于 c 类型:没有任何影响。       
 当未指定任何精度时,默认为 1。如果指定时不带有一个显式值,则假定为 0。 |

| .* | The precision is not specified in the format string but is placed as an additional integer argument before the argument 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, applying to integer specifiers (i, d, o, u, x, and X) and to the specifiers c (a wide character) and s (a wide-character string).
L The argument is interpreted as a long double (only applies to floating-point specifiers: e, E, f, g, and G).

Return Value

On success, returns the total number of characters written, not including the null character appended at the end of the string. On failure, returns a negative number.

Example

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

Example

#include

#include

int

main

(

)

{

   

char

str

[

80

]

;

   

sprintf

(

str

,

"The value of Pi = %f"

,

M_PI

)

;

   

puts

(

str

)

;

   

   

return

(

0

)

;

}

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


Pi 的值 = 3.141593

C Standard Library - <stdio.h> C Standard Library -