Does anyone know why if i put a printf just before a delay it waits until the delay is finished before it prints de message?
Code1 with sleep():
int main (void)
{
printf ("hi world");
system("sleep 3");
}
Code2 with a self implemented delay:
void delay(float sec)
{
time_t start;
time_t current;
time(&start);
do{
time(¤t);
}while(difftime(current,start) < sec);
}
int main (void)
{
printf ("hi world");
delay(3);
}
And if:
printf ("hi world");
delay(3);
printf ("hi world");
delay(3);
it waits until the sum of sleeps and then it prints the messages at the same time
Why does this happen?
UPDATE: I writed delay("sleep 3") when i called delay, i meant delay(3). Corrected
printf buffers it's output until a newline is output.
Add a fflush(stdout); to flush the buffers on demand.
the standard output is not flush until you output a '\n' char.
try printf ("hi world\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With