In C, Printing to stdout is easy, with printf
from stdio.h
.
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?
In C, Printing to stdout is easy, with printf
from stdio.h
.
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?
To print your context ,you can write code like this :
If you don't want to modify current codes and just for debug usage.
Add this macro:
Change
stderr
tostdout
if you want to roll back.It's helpful for debug, but it's not a good practice.
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.
Although initially both stdout and stderr come to the console, both are separate and can be individually redirected.
The syntax is almost the same as
printf
. Withprintf
you give the string format and its contents ie:With
fprintf
it is the same, except now you are also specifying the place to print to:Or in your case:
Examples:
Do you know
sprintf
? It's basically the same thing withfprintf
. The first argument is the destination (the file in the case offprintf
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.