Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

sleep() in C

Name: Anonymous 2012-09-03 0:27

So, I've been trying to use the sleep function in C, but I have a problem. If I use it once, like, for example:

printf("First Line.\n");
sleep(5);
printf("Second Line.");

Then it works just fine. But if I try doing that same thing, so:

printf("First Line.\n");
sleep(5);
printf("Second Line.\n");
sleep(5);
printf("Third Line.");

It just waits 10 seconds then displays the next two lines at once. Is there some work around for this?

Name: Anonymous 2012-09-03 0:39

fflush(stdout);

Name: Anonymous 2012-09-03 1:14

>>1
Are you sure you're terminating the second line with '\n'? On most Unix-like systems stdout is typically line-buffered when connected to a terminal, so on such systems the output won't be flushed until '\n' is encountered.

In any case, you could always call ``setbuf(stdout, 0);'' or fflush as >>2 mentioned.

Name: Anonymous 2012-09-03 5:13

be sure to fflush(stdin); too, lel

Name: Anonymous 2012-09-03 5:19

>>4
Undefined behaviour.

Name: Anonymous 2012-09-03 5:37

FLUSH MY ANUS

Name: Anonymous 2012-09-03 6:00

>>4
Caution: Repeated use may cause your terminal to become slightly moist.

Name: Anonymous 2012-09-03 6:49

MY ANUS IS ALREADY MOIST

Don't change these.
Name: Email:
Entire Thread Thread List