You are here

Delay function in C

Delay in C: delay function is used to suspend execution of a program for a particular time.

Declaration: void delay(unsigned int);

Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the "dos.h" header file which is not a part of standard C library.

Delay in C program

If you don't wish to use delay function then you can use loops to produce delay in a C program.

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.    int c, d;
  6.    
  7.    for (c = 1; c <= 32767; c++)
  8.        for (d = 1; d <= 32767; d++)
  9.        {}
  10.        
  11.    return 0;
  12. }

We have not written any statement in the loop body. You may write some statements that doesn't affect logic of the program.

C programming code for delay

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. main()
  5. {
  6.     printf("This C program will exit in 10 seconds.\n");        
  7.  
  8.     delay(10000);                        
  9.  
  10.     return 0;
  11. }

This C program exits in ten seconds, after the printf function is executed the program waits for 10000 milliseconds or 10 seconds and then it terminates.