echo '' ;

C Examples – Time Delay

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 second printf() 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 is 0 to 4,294,967,295 if its takes 4 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. So delay() 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:

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;
}

NEXT

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading