snprintf 函数用法详解-CSDN博客

网站介绍:文章浏览阅读6.5w次,点赞76次,收藏325次。使用snprintf函数时要注意两点,这是我看过无数使用这个函数的程序员绝大部分都存在的两点隐患:比如: char buf[LEN]; int n = snprintf(buf, LEN, "%s%d%s", str1, int2, str3); printf("total length n %d"\n, n);1.snprintf会自动在格式化后的字符串尾添加\0, 所有格式化长度,也就是第二个参数没有必要用LEN-1, 这样会浪费一个字节的空间。..._snprintf