Time delay in C
Last Updated :
18 Aug, 2022
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 than required clock run an empty loop. Here is implementation with a delay function.
C
#include <stdio.h>
#include <time.h>
void delay( int number_of_seconds)
{
int milli_seconds = 1000 * number_of_seconds;
clock_t start_time = clock ();
while ( clock () < start_time + milli_seconds)
;
}
int main()
{
int i;
for (i = 0; i < 10; i++) {
delay(1);
printf ("%d seconds have passed\n", i + 1);
}
return 0;
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...