PDF download Download Article PDF download Download Article

Did you ever want to make a C program wait for a certain time?

You can set up a technique to allow time to tick away, for example: when showing a splash page (a notice or hint) for a game.

Okay, here are some ways to make the program "stand still", read on...

Method 2
Method 2 of 2:

The "sleep()" Technique

PDF download Download Article
  1. Use sleep() The function called sleep(int ms) declared in <TIME.H>which makes the program wait for the time in milliseconds specified.
    • #include <TIME.H>
    • sleep(1000);
    • Change the "1000" to the number of milliseconds you want to wait (for example, if you want to make a 2 second delay, replace it with "2000".
    • Tip: On some systems the value might refer to seconds, instead of milliseconds. So sometimes 1000 isn't one second, but, in fact, 1000 seconds.
  2. Advertisement

Community Q&A

Search
Add New Question
  • Question
    On my computer, the sleep function works with seconds and I think it accepts integers. How can I drop a delay for half a second?
    Community Answer
    In the C language, sleep() accepts integers that represent the number of milliseconds the program should wait, which means you need to call sleep(500) to wait half a second.
Ask a Question
      Advertisement

      Sample Code

      A program that waits a given amount of seconds:


       #include 
       <stdio.h> 
        
       #include 
       <dos.h> 
        
       int 
       main 
       () 
       { 
       int 
       del 
       ; 
       // The delay period 
       printf 
       ( 
       "Enter the delay time (in seconds): " 
       ); 
       scanf 
       ( 
       "%i" 
       , 
       & 
       del 
       ); 
       del 
       *= 
       1000 
       ; 
       // Multiply it by 1000 to convert to milliseconds 
       delay 
       ( 
       del 
       ); 
       // delay. 
       printf 
       ( 
       "Done." 
       ); 
       return 
       0 
       ; 
       } 
      


      A program that counts down from 10 to 0:


       #include 
       <stdio.h> 
        
       #include 
       <time.h> 
        
       int 
       main 
       () 
       { 
       int 
       i 
       ; 
       for 
       ( 
       i 
       = 
       10 
       ; 
       i 
       >= 
       0 
       ; 
       i 
       -- 
       ) 
       { 
       printf 
       ( 
       "%i 
       \n 
       " 
       , 
       i 
       ); 
       // Write the current 'countdown' number 
       delay 
       ( 
       1000 
       ); 
       // Wait a second 
       } 
       return 
       0 
       ; 
       } 
      

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Warnings

      • If you are using the for-loop, the compiler may optimize the code, and, because the loop does nothing, remove it. This doesn't happen when using delay().
      • Note that when using the for-loop technique, you might need a very big span for i, because an empty statement is executed very fast. Such big numbers may not fit in an integer type.
      • This technique is generally useless in anything besides a trivial program. In general, use timers or an event-driven approach to implement this. Otherwise the program will become unresponsive during the delay time, and that's not always a good thing. Besides, choosing N in your loop, if it depends on instruction execution, may have surprising results. Apparently the original author has never heard of an optimizing compiler...it may optimize away the entire loop if it actually does nothing !
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 398,954 times.

      Is this article up to date?

      Advertisement