mirror of
https://github.com/wangdoc/clang-tutorial.git
synced 2026-02-03 19:03:44 +08:00
docs(string): fixed snprint()'s return value #54
This commit is contained in:
@@ -534,7 +534,7 @@ snprintf(s, 12, "%s %s", "hello", "world");
|
||||
|
||||
上面的例子中,`snprintf()`的第二个参数是12,表示写入字符串的最大长度不超过12(包括尾部的空字符)。
|
||||
|
||||
`snprintf()`的返回值是写入变量的字符数量(不计入尾部的空字符`\0`),应该小于`n`。如果遇到错误,返回负值。
|
||||
`snprintf()`的返回值是写入格式字符串的字符数量(不计入尾部的空字符`\0`)。如果`n`足够大,返回值应该小于`n`,但是有时候格式字符串的长度可能大于`n`,那么这时返回值会大于`n`,但实际上真正写入变量的还是`n-1`个字符。如果遇到错误,返回一个负值。因此,返回值只有在非负并且小于`n`时,才能确认完整的格式字符串写入了变量。
|
||||
|
||||
## 字符串数组
|
||||
|
||||
|
||||
Reference in New Issue
Block a user