How to Delay in C

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...

Method 1 of 2:

The "for-loop" technique

  1. Picture 1 of How to Delay in C
    Use a typical "for" loop followed by a null statement to implement time delay.
  2. Picture 2 of How to Delay in C
    Write as follows, for an example:
    1. for (i=1 ; i
    2. The statement followed by the ";" makes the computer execute the loop 100 times without any noticeable event. It just creates a time delay.
Method 2 of 2:

The "sleep()" Technique

  1. Picture 3 of How to Delay in C
    Use sleep() The function called sleep(int ms) declared in which makes the program wait for the time in milliseconds specified.
  2. Picture 4 of How to Delay in C
    Include the following line in your program before "int main()":
    1. #include
  3. Picture 5 of How to Delay in C
    Insert, wherever you need your program to make a delay:
    1. sleep(1000);
    2. 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".
    3. 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.

You've just finished reading the article "How to Delay in C" edited by the TipsMake team. You can save how-to-delay-in-c.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.

« PREV : How to Set Up SDL...
How to Create a... : NEXT »