snprintf是否会自动将字符串末尾填''?

snprintf是否会自动将字符串末尾填''?,第1张

Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf.

If len <count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.

If len >count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

"如果字符串很长,超过了buffer的size,是否会截断后还是在buffer末尾添'\0'?"

If len >count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

回答完毕。

以上是vc里ok,linux下的snprintf截断后会添加'\0',这个要注意。

这个和 *** 作系统没有直接联系 和语言编译器有关.

C/C++编译器在Linux通常gcc/g++ 在编译的时候自动会给用“”的字符串加‘\0’

字符串截断符和语言有关.

c语言字符串的结束符是 '\0' 也就是ASCII 码 0

不知道你注意到没有,我没有加 Linux,是因为这时 C 语言规定,跟 *** 作系统无关,在 Windows 下也是一样


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/9021167.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-24
下一篇 2023-04-24

发表评论

登录后才能评论

评论列表(0条)

保存