奇思妙想 · 2024年 4月 7日 0

printf返回值有意义吗?

45 次浏览

利用man 3 printf获取返回值的解释:Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings). 这里提到是返回被成功输出的字符个数,这里的字符特指字节。一般而言,写代码不用特意判断printf的返回值,如果有错误,很容易看结果得到错误,如果特意判断返回值反而让代码更冗余复杂不易维护。要注意,kernel可能提供读写节点,对应的代码会计算真实读写的字节数,可能利用一些printf的变体函数,必须准确记录返回值,否则会发生错误。