sleep() inside a loop.

I have defined sleep() in my program. I am using it in a loop. But I observed that sleep() is adding up the total amount of delay (no of times loop executes) and only sleeps for this total amount of time in the beginning of execution. After that loop executes normally completely ignoring sleep() function. Same happened when I used window's Sleep(). Can any one pls help? I want sleep() to be called every time it appears in the loop and not just once in the beginning for total added delay. Thanks in advance.
maybe more easier to understand if you paste your some code...^_^
its a sample code taken from this site. its here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
  int n;
  printf ("Starting countdown...\n");
  for (n=10; n>0; n--)
  {
    printf ("%d\n",n);
    wait (1);
  }
  printf ("FIRE!!!\n");
  return 0;
}


what is happening is instead of printing nos every 1 sec prog waits for total 10 secs(loop count) and then displays all nos 10 to 1 without any delay.


stdout is being buffered. Add fflush( stdout ); between lines 18 and 19.
@jsmith... hey thanks its wrking now. :-) Ill read more on stdout buffering.
@ubootme... thanks :-)
Topic archived. No new replies allowed.