#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
The output of above on my machine is
hello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-err
I had to kill the program to stop it.
Is it the correct and expected behavior.
Or it is wrong.This was an interview question hence I am posting here.
This is the expected output:
while (1)loop.stdoutis line-buffered (by default), so it only flushes to the console when a new-line character ('\n') is printed. As you aren’t printing any new-line characters, you never see any of thehello-outtext.stderris not line-buffered (by default), so it updates the console on every newfprintf()call.