snprintf函数说明

int snprintf(char *restrict buf, size_t n, const char * restrict   format, ...);

函数说明:最多从源串中拷贝 n 1 个字符到目标串中,然后再在后面加一个 0 。所以如果目标串的大小为 n

                 的话,将不会溢出。

函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值。

Result1( 推荐的用法 )


 

root] /root/lindatest
$ ./test
str=012345678

 

 

Result2:(不推荐使用)


 

root] /root/lindatest
$ ./test
str=01234567890123456

 

 

snprintf函数返回值的测试:


[root] /root/lindatest
$ ./test
aaabbbccc length=9
str1=abc,ret1=3
str2=aaa,ret2=9

原文地址:https://www.cnblogs.com/hjslovewcl/p/2314360.html