【问题标题】:When to use printf vs fprintf(stderr) [duplicate]何时使用 printf vs fprintf(stderr) [重复]
【发布时间】:2023-04-09 15:51:02
【问题描述】:

以下两种说法有区别吗:

fprintf(stderr, "Hello this is something\n");
printf("Hello this is something\n");

如果有,有什么区别,什么时候用一个而不是另一个?

【问题讨论】:

  • 如果是错误信息,应该正确输出到stderr,正常输出到stdout。另外,如果没有转换说明符,则不需要(f)printf,只需使用fputs,例如fputs ("error: Hello this is something -- failed\n", stderr);

标签: c


【解决方案1】:

对于您的特定 2 Ines 代码,不,它们不一样。

printf 只写入标准输出(不是标准错误)

当您想写入不同的流而不是标准输出时,您可以使用 fprintf。

【讨论】:

    猜你喜欢
    • 2010-09-05
    • 1970-01-01
    • 2020-08-12
    • 2012-08-19
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 2021-02-04
    相关资源
    最近更新 更多