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?



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);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);Or in your case:

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



Some examples of formatted output to stdout and stderr:

printf("%s", "Hello world\n"); // "Hello world" on stdout (using printf)fprintf(stdout, "%s", "Hello world\n"); // "Hello world" on stdout (using fprintf)fprintf(stderr, "%s", "Stack overflow!\n"); // Error message on stderr (using fprintf)


#includeint main ( ) { printf( "hello " ); fprintf( stderr, "HELP!" ); printf( " world\n" ); return 0;}$ ./a.exeHELP!hello world$ ./a.exe 2> tmp1hello world$ ./a.exe 1> tmp1HELP!$stderr is usually unbuffered and stdout usually is. This can lead to odd looking output like this, which suggests code is executing in the wrong order. It isn"t, it"s just that the stdout buffer has yet to be flushed.Redirected or piped streams would of course not see this interleave as they would normally only see the output of stdout only or stderr only.

You are watching: How to print to stderr in c

Although initially both stdout and stderr come to the console, both are separate and can be individually redirected.

Do you know sprintf? It"s basically the same thing with fprintf. The first argument is the destination (the file in the case of fprintf i.e. stderr), the second argument is the format string, and the rest are the arguments as usual.

I also recommend this printf (and family) reference.

If you don"t want to modify current codes and just for debug usage.

Add this macro:

#define printf(args...) fprintf(stderr, ##args)//under GCC#define printf(args...) fprintf(stderr, __VA_ARGS__)//under MSVCChange stderr to stdout if you want to roll back.

It"s helpful for debug, but it"s not a good practice.

To print your context ,you can write code like this :

FILE *fp;char *of;sprintf(of,"%s%s",text1,text2);fp=fopen(of,"w");fprintf(fp,"your print line");
Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

See more: Customer Reviews: Etude House Moistfull Collagen Water Jelly Cream 75Ml

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged c printf stderr or ask your own question.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.10.22.40552

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.