-1

This code work's fine :

int main()
{
while(1){

printf("hi\n");
sleep(1);
}

return 0;
}

Output: hi hi hi ...

but when we remove '\n' it will print nothing?

int main()
{
while(1){

printf("hi");
sleep(1);
}

return 0;
}

Output: NO OUTPUT

Plz anyone give explaination of this behaviour :)

saiflash
  • 3
  • 3
  • 2
    Check this: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – babon Jul 18 '16 at 06:56

2 Answers2

1

stdout if buffered by default, so to flush it you should out \n or fill that internal buffer completely. If such behavior is undesired - you can flush it manually with fflush(stdout) or turn off buffering with setbuffer(stdout, NULL, 0).

Sergio
  • 8,099
  • 2
  • 26
  • 52
0

The reason you're not seeing anything printed is because the output is buffered.

The reason you see prints when you suffix \n is that on some systems, a newline causes the buffer to flush. You can flush stdout manually like so:

int main()
{
    while(1)
    {
        printf("hi");
        fflush(stdout);
        sleep(1);
    }
return 0;
}
Ishay Peled
  • 2,783
  • 1
  • 23
  • 37