c - perror 和 fprintf 到 stderr 之间有什么区别?

标签 c linux

我知道两者都可以作为错误消息,

perror(" ");
fprintf(stderr, " ");

但是它们之间真正的区别是什么?各自的优点/缺点是什么?

最佳答案

the perror() standard :

The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the standard error stream as follows:

  • First (if s is not a null pointer and the character pointed to by s is not the null byte), the string pointed to by s followed by a <colon> and a <space>.

  • Then an error message string followed by a <newline>.

The contents of the error message strings shall be the same as those returned by strerror() with argument errno.

所以,

perror( " " );

会发出类似的东西

 : invalid argument

stderr,具体取决于 errno 的当前值。

但是

fprintf( stderr, " " );

将仅向 stderr 发出空格字符,而不打印当前 errno 值的字符串表示形式。

fprintf( stderr, ""); 单独来看是非常无用的。 perror( ""); 将提供有关 errno 当前值的数据。

关于c - perror 和 fprintf 到 stderr 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40677589/

相关文章:

c++ - 如何在 net-snmp 中打印整数和 Counter32 值

linux - 读取目录(readdir)和目录查找操作的区别

c - 如何实现一个函数以在其本地范围之外使用?

c - 使用 malloc 为数组插入值

c - 按字母顺序对单词数组进行排序

c - void function bubble_sort 不对数组进行排序

c - 是write系统调用减少内存写周期如何处理

将 const char* 转换为 LPCTSTR

linux - Apache HTTP 服务器在哪里存储传入请求的数据

c - 标记数据包以通过原始套接字发送