Introduction :
In the early days of computer programming, the DOS operating system was a popular platform for software development. That time, the programmer uses the dos.h library to interact with the system and control various aspects of program execution. In the computer programming, time management plays an important role in various applications.
One of the important functions of the dos.h library is delay() function, which allowed programmers to introduce time delays into their applications or programs. In this article, I shall explore you how to use the delay() function of the dos.h library in the Turbo C++ IDE using C or C++ programming languages.
What is delay() function :
The delay() function is not part of the standard in modern C or C++ programming language. It is specific to the Turbo C++ IDE that support the dos.h library. The delay() function is part of the dos.h library, which allowed you to introduce a time delay in your programs. The delay() function is used to pause or sleep the program for a specified amount of time.
Using delay() function, you can create different type of program such as a simple animation program, basic game, painting or drawing program and clock or timer program. It is used to pause and control the speed of output of the program.
syntax of delay() function :
The following code is the syntax of delay() function.
void delay(unsigned int milliseconds);
The delay() function takes an unsigned integer specifying the duration of the delay in milliseconds. To use the delay() function, simply call it with the number of milliseconds you want the program to pause.
Use of delay() function :
In this article, I show you two programs using delay() function such as console program and graphics program.
Console program using delay() function :
The program is a simple console animation where you can display the message “puskarcoding.com 1” to “puskarcoding.com 10” with a one-second delay between each message. After print all the message, press any key for stopping the program. It is a console-based program so the output will be displayed on the console.
Explanation of the program :
First, you have to include neccecary header files in the program. The dos.h header file or library is specific to DOS-based systems. The dos.h header file provides functions and constants related to low-level hardware operations and system functions like delay() function. Then include stdio.h for use printf() function and also include conio.h for use clrscr() and getch() functions.
In the main() function, declares an integer variable “i”, which will be used as a loop counter. Then, clears the console screen using clrscr() function. The clrscr() function removes any previous text or output from the screen. In the for loop, print the message “puskarcoding.com” 10 times on the console using printf() function. Here, I use delay() function to introduce a delay of 1000 milliseconds (1 second) between each print statement. After that I use getch() function for waits for a key press from the user before stopping the program using closegraph() function.
Code of the console program using delay() function :
/*Developed by Puskar Jasu*/
#include <dos.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
clrscr();
for (i = 0; i < 10; i++)
{
printf("puskarcoding.com %d\n", i + 1);
delay(1000);
}
getch();
return 0;
}
Output :
When you run the program, you see the output on your pc.

Graphics program using delay() function :
The program is a simple graphics program where you can draw circle and rectangle in the graphics window with 1 second interval. After drawing and displaying the shapes, press any key for stopping the program. It is a graphics-based program so the output will be displayed on the graphics mode or window.
Explanation of the program :
First, you have to include neccecary header files in the program. The dos.h header file or library is specific to DOS-based systems. The dos.h header file provides functions and constants related to low-level hardware operations and system functions like delay() function. Then include graphics.h for use graphical functions and conio.h for getch() functions.
In the main() function, declares two integer variables “graphic_driver” and “graphic_mode” which will be used for initializing the graphics mode. You have to also declare two integer variables “x_max” and “y_max” which will be used to store the maximum x and y coordinates of the graphics window. Now, initializes the graphics system using initgraph() function with proper arguments. Next, retrieves the maximum x and y coordinate of the graphics window using getmaxx() and getmaxy() functions and stores the value in the “x_max” and “y_max” variables respectively.
You can use setbkcolor() and setcolor() functions to set the background color and the drawing color of the graphics window respectively. After that, draws a circle with a radius of 100 pixels using circle() function. Then use delay() function to introduce a delay of 1000 milliseconds (1 second) to pause the program. Using rectangle() function, draws a rectangle on the screen. Last, use getch() function to get a key press from the user before closing the graphics window and releases resources using closegraph() function.
Code of the graphics program using delay() function :
/*Developed by Puskar Jasu*/
#include <dos.h>
#include <graphics.h>
#include <conio.h>
int main()
{
int graphic_driver = DETECT, graphic_mode;
int x_max, y_max;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
x_max = getmaxx();
y_max = getmaxy();
setbkcolor(4);
setcolor(9);
circle(x_max / 4, y_max / 2, 100);
delay(1000);
rectangle(x_max * 3 / 4 - 100, y_max / 2 - 100, x_max * 3 / 4 + 100, y_max / 2 + 100);
getch();
closegraph();
return 0;
}
Output :
When you run the program, you see the output on your pc.

How run the program :
At first, install the Turbo C++ IDE on your pc. After that, open the Turbo C++ IDE. Now, create a C or C++ file with .c or .cpp extension. Next, copy the code of the program and paste into your C or C++ file in the Turbo C++ IDE. If you do not know how to copy paste in the Turbo C++ IDE, click here. You can also see how to use graphics.h in Turbo C++ IDE from this link. After compile, run the program on your pc to see how use delay() function using C or C++ programming language.
Conclusion :
In this above article, I have explored the fundamentals of the delay() function of dos.h library such as syntax, purpose and usage. The delay() function allowed programmers to pause program execution for a specified duration to create basic animations, games and time-base programs. Thank you for visiting my site.