Code
printf("Doing functionname... ");
if (functionname(args) == -1)
{
perror("functionname");
}
else
{
printf("ok\n");
}
Expected behavior
Doing functionname… ok
OR
Doing functionname… functionname: Error blah blah
Actual behavior
However stderr and stdout are different output streams so the result looks like this,
functionname: Error blah blah
Doing functionname…
What are the possible workarounds?
Flush the output stream.
stdout, by default, is line buffered; so the output is only effectivelly written when it sees a ‘\n’ (or when the buffer gets filled). By contrast,stderris not buffered by default, so every single character is output immediately.If you terminate the string with a ‘\n’, the stream will be flushed with no need for a specific fflush statement.
Alternatively, print your information stuff to the
stderrstreamThis has the advantage that users may redirect all information messages to /dev/null