Following is the code snippet.
int main()
{
int i =0;
while (i++ < 5)
{
// Do some heavy processing
printf("i = %d\n", i);
sleep(1);
}
}
The heavy processing part is doing its part every 1 second.
I want to display ‘i’ after 1 s as well but its displaying the entire output after its done. I know using a while is not a very elegant way to do this, but it seemed easier. Which is the optimal way of achieving this?
I am running the code in unix, gcc complier
You need to flush stdout; you can use fflush(3) to do so:
You could also disable buffering entirely on stdout: