c语言snprintf函数
C语言中的snprintf()函数 (snprintf() function in C)
The snprintf() function is defined in the <stdio.h> header file.
snprintf()函数在<stdio.h>头文件中定义。
Prototype:
原型:
int snprintf(char *str, size_t size, const char *format, ...);
Parameters:
参数:
str - is a Buffer.
str-是一个缓冲区。
size - is a maximum number of bytes.
size-是最大字节数。
format - c string that contains a format that follows the same specifications as format in printf.
format -c字符串,其格式遵循与printf中相同的格式。
... - the optional (...) arguments are just the string formats like “%d” as seen in printf.
...-可选的( ... )参数只是字符串格式,如printf中所示的“%d”。
Return type: int
返回类型: int
Use of function:
使用功能:
The snprintf() formats and stores a series of characters and values in the array buffer.
snprintf()格式化并将一系列字符和值存储在数组缓冲区中。
It redirects the output of printf to the buffer.
它将printf的输出重定向到缓冲区。
Using snprintf() build a string once and use %s instead of %d, %s, %f, %ld every time.
使用snprintf()一次构建一个字符串,并每次使用%s代替%d , %s , %f和%ld 。
C中的snprintf()示例 (snprintf() example in C)
#include <stdio.h>
int main() {
char* r = "Includehelp.com";
char buf[100];
// in buffer using snprintf
int i = snprintf(buf, 12, "%s\n", r);
// Print the string stored in buffer and
// character count
printf("string is:\n%s\ncharacter count = %d\n", buf, i);
return 0;
}
Output
输出量
string is:
Includehelp
character count = 16
翻译自: https://www.includehelp.com/c-programs/snprintf-function-in-c-language-with-example.aspx
c语言snprintf函数