C中perror函数两个输出的执行顺序之间的差异



相同代码上的输出序列之间的差异;\n〃;

#include<stdio.h>
#include<errno.h>
#include<string.h>

int main()
{
FILE * fp;
fp = fopen("GeeksForGeeks", "/root/C");
printf("Value of error no : %dn", errno);
printf("The error message is : %sn", strerror(errno));
perror("Message from perror.");
return 0;
}

这是我的代码,但当我删除";\n〃;,perror的执行顺序发生了变化。有人能解释一下原因吗?

很可能,stdout处于行缓冲模式,因此它在n上刷新,而perror写入stderr

perror调用之前添加fflush(stdout);将修复它。

最新更新