sprintf
int sprintf ( char * str, const char * format, ... );
Write formatted data to string
Composes a string with the same text that would be printed if
format was used on
printf, but instead of being printed, the content is stored as a
C string in the buffer pointed by
str.
The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version). //buffer的大小必须能够包含format后形成的字符串的长度,不然会溢出。
A terminating null character is automatically appended after the content. //自动添加'\0'
After the format parameter, the function expects at least as many additional arguments as needed for format.
Parameters
-
str
-
Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string.
format
- C string that contains a format string that follows the same specifications as format in printf (see printf for details). ... (additional arguments)
-
Depending on the
format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a
format specifier in the
format string (or a pointer to a storage location, for
n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
Return Value
On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.On failure, a negative number is returned.
返回值是形成字符串以后,该字符串的长度(不包括自动添加的空字符),例如下面的例子 "%d plus %d is %d"形成的字符串为" 5 plus 3 is 8 ",因此返回值为13。
Example
| |
Output:
[5 plus 3 is 8] is a string 13 chars long |
snprintf
int snprintf ( char * s, size_t n, const char * format, ... );
Write formatted output to sized buffer
Composes a string with the same text that would be printed if
format was used on
printf, but instead of being printed, the content is stored as a
C string in the buffer pointed by
s (taking
n as the maximum buffer capacity to fill).
If the resulting string would be longer than n-1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function. //snprintf的返回值是欲写入的字符串长度,而不是实际写入的字符串长度
A terminating null character is automatically appended after the content written.After the format parameter, the function expects at least as many additional arguments as needed for format.
Parameters
-
s
-
Pointer to a buffer where the resulting C-string is stored.
The buffer should have a size of at least n characters.
n
-
Maximum number of bytes to be used in the buffer.
The generated string has a length of at most n-1, leaving space for the additional terminating null character. //最多拷贝n-1个字符
size_t is an unsigned integral type.
format
- C string that contains a format string that follows the same specifications as format in printf (see printf for details). ... (additional arguments)
-
Depending on the
format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a
format specifier in the
format string (or a pointer to a storage location, for
n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
Return Value
The number of characters that would have been written if n had been sufficiently large, not counting the terminating null character.If an encoding error occurs, a negative number is returned.
Notice that only when this returned value is non-negative and less than n, the string has been completely written.
Example
| |
Output:
The half of 60 is 30, and the half of that is 15. |
For more examples on formatting see printf.