Time delay is necessary for some cases in programming. for example, if you wanna print some string with some delay or wanna write some value in file frequently with some delay or need to check the excel file value with some delay like many use cases. To make a delay in application execution, we have two options like can write own custom user to define a function as well as can use some pre-defined library functions in C. In many ways, users can write the time delay functions like using for loop, while loop, do-while loop as well as a separate function in all. Here will discuss about C Examples Time Delay.
Windows Platform
sleep() function in Windows.h: If your application runs on the Windows platform, then you can use the sleep()
in your code. Please refer to this post
C Program
Some delay using for while loop and Clock function
Code:
#include <stdio.h> #include <time.h> void delay(int sec) { while (sec> clock()); } int main() { int seconds=0,delay_time=0; printf("Hello User, Enter time delay (In sec): "); scanf("%d",&seconds); //Update the current clock time in delay_time variable. delay_time = clock(); // Convert sec to milli_sec for call delay function delay_time += 1000 *seconds; delay(delay_time); printf("Printing after %d seconds.\n",seconds); return printf("Welcome to www.ArunEworld.com"); }
Output:
Hello User, Enter time delay (In sec): 6 Printing after 6 seconds. Welcome to www.ArunEworld.com -------------------------------- Process exited after 7.5 seconds with return value 29 Press any key to continue . . .
Some delay using for loop
Code:
#include<stdio.h> void delay() { unsigned int i; for(i=0; i<100000000; i++); } void main() { printf("Hello User\n"); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); printf("Welcome to ArunEworld"); }
Output:
Hello User Welcome to ArunEworld -------------------------------- Process exited after 4.465 seconds with return value 21 Press any key to continue . .
Explanation:
delay()
is a void type and it won’t return any values.- In the first
printf()
, printing the “Hello user
” string and the secondprintf()
will print the “Welcome to ArunEworld
” string. - But first string printing after some time taking for print the second string, because in many times called the
delay()
function (16 Times called). - Almost it will take a 2-second delay in between two strings printing. If you have a question like what is the time delay for one time execute the
delay()
? The answer is based on the system clock speed it will vary. - Also in the above code used
unsigned int
type this range is0 to 4,294,967,295
if its takes4 bytes
. - This program takes 1.78 seconds for complete execution without any delay.
- If you observe the out in the above example its takes 4.465 seconds for with
delay()
16 times. Sodelay()
after 16 times, it consuming 2.685 Seconds.
Linux/Unix
Using sleep
Function
#include <stdio.h> #include <unistd.h> // for sleep function int main() { printf("This is before the delay.\n"); // Sleep for 2 seconds sleep(2); printf("This is after the delay.\n"); return 0; }
Use of Time Delays
Time delays in programming are often used for various purposes, including:
Use | Notes |
---|---|
Animation and Visual Effects: | Adding delays can create smooth animations or visual effects in graphical applications. |
User Interaction: | Introducing delays can be useful for simulating pauses in user interaction, giving users time to read messages or process information. |
Timing and Synchronization: | Delays are employed to control the timing of actions or synchronize activities in a program. |
Polling and Waiting: | In scenarios where a program is waiting for an external event (e.g., user input, data from a sensor, network response), time delays may be used in a loop to periodically check for the event. |
Throttling or Rate Limiting: | Delays can be used to control the rate at which a program performs certain actions, preventing it from overwhelming system resources or external services. |
Simulation and Testing: | In simulations or testing scenarios, delays can mimic real-world scenarios, such as the time it takes for a process to complete. |
Game Development: | Time delays are often used in games to control the pace of gameplay, create suspense, or simulate time-based events. |
Timeouts: | Introducing timeouts with delays is common when dealing with operations that may take an unpredictable amount of time. If an operation doesn’t complete within a specified time, the program can take appropriate action. |
Here below are three C Examples Time Delay
Wait for User Input
#include <stdio.h> #include <unistd.h> int main() { printf("Please wait for 3 seconds...\n"); sleep(3); printf("Thank you for waiting!\n"); return 0; }
Simulation of Real-Time System
#include <stdio.h> #include <unistd.h> int main() { for (int i = 1; i <= 5; ++i) { printf("Processing step %d...\n", i); sleep(2); // Simulate processing time } printf("Process completed!\n"); return 0; }
Animation Frame Rate Control
#include <stdio.h> #include <unistd.h> int main() { for (int frame = 1; frame <= 10; ++frame) { // Render the frame printf("Rendering frame %d...\n", frame); // Control frame rate (e.g., 1 frame per second) sleep(1); } return 0; }