C 库函数 - snprintf()

C 标准库 - <stdio.h>
声明
下面是 snprintf() 函数的声明。
int snprintf ( char * str, size_t size, const char * format, … );
参数
str – 目标字符串。
size – 拷贝字节数(Bytes)。
format – 格式化成字符串。
… – 可变参数。
返回值
(1) 如果格式化后的字符串长度小于等于 size,则会把字符串全部复制到 str 中,并给其后添加一个字符串结束符 \0;
(2) 如果格式化后的字符串长度大于 size,超过 size 的部分会被截断,只将其中的 (size-1) 个字符复制到 str 中,并给其后添加一个字符串结束符 \0,返回值为欲写入的字符串长度。

快速上手参考菜鸟教程[https://www.runoob.com/cprogramming/c-function-snprintf.html]

其他一些注意点参考《C语言snprintf()函数:将格式化的数据写入字符串—sprintf()》[http://c.biancheng.net/cpp/html/2417.html]

这里就有snprintf()与sprintf()使用方法类似,请参考:C语言sprintf()函数[http://c.biancheng.net/cpp/html/295.html]
在GCC中,该函数名称就snprintf(),而在VC中称为_snprintf()。由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可能会有差异。

这里就有GCC、VC的差异。

猜你喜欢

转载自blog.csdn.net/skhhongtu/article/details/121874780
今日推荐