如何在 C 中打印到 stderr? [英] How I can print to stderr in C?

查看:86
本文介绍了如何在 C 中打印到 stderr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中,使用 stdio.h 中的 printf 打印到标准输出很容易.

In C, Printing to stdout is easy, with printf from stdio.h.

但是,如何打印到stderr?我们显然可以使用fprintf 来实现它,但它的语法似乎很奇怪.也许我们可以使用 printf 打印到 stderr?

However, how can print to stderr? We can use fprintf to achieve it apparently, but its syntax seems strange. Maybe we can use printf to print to stderr?

推荐答案

语法与 printf 几乎相同.使用 printf,您可以提供字符串格式及其内容,即:

The syntax is almost the same as printf. With printf you give the string format and its contents ie:

printf("my %s has %d chars\n", "string format", 30);

fprintf 相同,除了现在您还指定要打印到的位置:

With fprintf it is the same, except now you are also specifying the place to print to:

File *myFile;
...
fprintf( myFile, "my %s has %d chars\n", "string format", 30);

或者在您的情况下:

fprintf( stderr, "my %s has %d chars\n", "string format", 30);

这篇关于如何在 C 中打印到 stderr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆