Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Time delay in C

  • Difficulty Level : Easy
  • Last Updated : 18 Dec, 2019

In this post, we will see how to give a time delay in C code. Basic idea is to get current clock and add the required delay to that clock, till current clock is less then required clock run an empty loop.

Here is implementation with a delay function.

// C function showing how to do time delay
#include <stdio.h>
// To use time library of C
#include <time.h>
void delay(int number_of_seconds)
    // Converting time into milli_seconds
    int milli_seconds = 1000 * number_of_seconds;
    // Storing start time
    clock_t start_time = clock();
    // looping till required time is not achieved
    while (clock() < start_time + milli_seconds)
// Driver code to test above function
int main()
    int i;
    for (i = 0; i < 10; i++) {
        // delay of one second
        printf("%d seconds have passed\n", i + 1);
    return 0;


This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!