It's a basic question, but i can't find a good answer.
For example, if i use malloc
, i check the malloc
and if i saw an error i can do :
fprintf(stderr, "Message");
It's an important error message, the output is on stderr
.
Now if i have a program who ask numbers between 1 an 5. The program ask to the user until he enters a good number.
If the user enter 10 for example, is it good things to redirect to error message to stderr
?
fprintf(stderr, "Message, wrong number, try again");
Or it's a normal error, and the message should go on stdout
?
Remember that stderr is not really about errors, but about diagnostics. Any diagnostic messages (including debug mode notifications and fancy progress bars) should go to stderr, while all the interactions, be it user or another program, should go on stdout. Also stderr is normally connected to a terminal, while stdout may be part of a shell pipe (thus invisible). And there is no way to connect stderr to next program separately from stdout.
Regarding your case, the message about wrong number is obviously the part of interaction, so use stdout. It is bad idea to move active conversation to other stream. But if it is not intended that user/peer process should read that and react, but is just info for someone diagnosing/debugging the program, then use stderr.