0
#include<stdio.h>
int main()
{   int n=3;
    while(n>0)
    {   printf("P");
        n--;
        sleep(1);
    }
    return 0;
}

With this code 3 P's are appearing at the same time after 3 seconds. But I want it to appear one by one with 1 second time interval. How should I modify it?

uday
  • 8,544
  • 4
  • 30
  • 54
  • 2
    Possible duplicate of [Why does sleep() execute before my printf(), the opposite order of my code?](http://stackoverflow.com/questions/338273/why-does-sleep-execute-before-my-printf-the-opposite-order-of-my-code) – indiv Mar 20 '17 at 18:34

2 Answers2

1

The problem is the buffer is not flushed by printf. To do so, you can either print '\n' after your P : printf("P\n");

Either call fflush on stdout :

printf("P");
fflush(stdout);
Omar
  • 943
  • 6
  • 9
0

You mean this one :

#include<stdio.h>
int main(){  

 int n=3;
 sleep(1);

    while(n>0){   
        printf("P");
        n--;
        sleep(1);
      }
return 0;
}
ChrisK
  • 41
  • 5