当前位置:网站首页>The difference between scnprintf and snprintf

The difference between scnprintf and snprintf

2022-06-22 09:10:00 zuhan_ twenty million two hundred and ten thousand three hundre

int snprintf(char *buf, size_t size, const char *fmt, ...)
int scnprintf(char *buf, size_t size, const char *fmt, ...)

Set the variable to a parameter (…) according to format Format as a string , Then copy it to str in .
(1) If the length of the formatted string < size, Then copy all of this string to str in , And add a string terminator to it (’\0’);
(2) If the length of the formatted string >= size, Only one of the (size-1) Characters copied to str in , And add a string terminator to it (’\0’), The return value is the length of the string to be written .

scnprintf() and snprintf() They don't cross the line , And will add a terminator on the last side ’\0’, The size of the returned value does not contain the last terminator , Will be truncated , Here's the difference :
scnprintf() Returns that has been written to buf The number of characters , and snprintf() Returns the length of the formatted character ( About to be written to buf The number of characters , Maybe it's better than size Be big ).
It is generally recommended to use scnprintf()

Reference link :https://blog.csdn.net/Namcodream521/article/details/85239473
https://blog.csdn.net/zhy025907/article/details/52494923

原网站

版权声明
本文为[zuhan_ twenty million two hundred and ten thousand three hundre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220904438457.html