wangdoc/clang-tutorial

snprintf 返回值超过 n

bdoycn opened this issue · 1 comments

文中写到 snprintf 返回值应该小于 n
但是实际测试会超过 返回格式化后的总字符长度

代码

size_t size = snprintf(s, 5, "%s%s33", "11", "22");

printf("size :>> %zu\n", size);
puts(s);

返回值

size :>> 6
1122

编译器

Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin19.6.0

我也查了几个文档
c 的文档好像没有详细说
https://en.cppreference.com/w/c/io/fprintf
有一个 c++的文档好像是说超过的字符会计入返回值
http://www.cplusplus.com/reference/cstdio/snprintf/

谢谢指出,我已经改过了。