10: int main( void )
11: {
12: int ctr;
13: int wait = 13;
14:
15: /* Pause for a number of seconds. Print a *
16: * dot each second waited. */
17:
18: printf(“Delay for %d seconds\n”, wait );
19: printf(“>”);
20:
21: for (ctr=1; ctr <= wait; ctr++)
22: {
23: printf(“.”); /* print a dot */
24: fflush(stdout); /* force dot to print on buffered machines */
25: sleep( (int) 1 ); /* pause 1 second */
26: }
27: printf( “Done!\n”);
28: return (0);
29: }
30:
31: /* Pauses for a specified number of seconds */
32: void sleep( int nbr_seconds )
33: {
34: clock_t goal;
35:
36: goal = ( nbr_seconds * CLOCKS_PER_SEC ) + clock();
37:
38: while( goal > clock() )
39: {
40: ; /* loop */
41: }
42: }
This program contains a function you might find useful in other programs you write. The
sleep()function pauses the computer for a number of seconds. During this time, the
computer simply checks to see if it has waited long enough. Once the specified number
of seconds has elapsed, the function returns control. You’ll find that this function, or vari-
ations of this function, can be used in a number of ways. Because of the speed of com-
puters, you will often want to pause the computer so that the reader has time to read
information you display. For example, you might want to display a copyright screen
when you first start an application.
As a simple illustration, the main part of this program prints dots. Between each dot, the
program pauses for one second by using the sleep()function just described. For fun,
220 Type & Run 3
LISTINGT&R 3 continued
16 448201x-T&R3 8/13/02 11:16 AM Page 220