gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:22 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com