In this article, we look into the delay() function in C++. There can be many cases where we need to create a delay in our programs before exiting out of execution. We can use a delay for this purpose in our code which is used to delay the program’s exit time. We can call the delay () function when we want to exit our program after a given time. This is just one of the extra features provided in the older versions of C/C++. This function is declared in the “dos.h” header file, we need to import this file in C/C++ program.

Note: The “dos.h” header file is only available in Turbo C3 Compiler so the program must be compiled in Turbo C Compiler or your compiler must have “dos.h” header file in it.

Syntax:

void delay (unsigned int)

  • void: The function has no return value.
  • unsigned int: Permissible Range of values: 0 to 4,294,967,295 (Positive Number).

The function accepts values in milliseconds format. So, if we want to create a delay of 1 second, we pass 1000 as a parameter. The following program is compiled and executed in Turbo C++ Compiler.

C++ Program to Demonstrate delay() Function

#include<iostream.h>
#include<dos.h> // to import delay function
#include<conio.h>

void main ()
{
	clrscr(); //clears screen
	cout<<” Hello” <<endl;
	cout<<” This program will exit in 5 seconds”;
	delay (5000); 
	cout<<” This line will not be printed”;

}

Output:

C++ Delay Function

Explanation:

In the above example, we used delay() function after line 7 and gave it a parameter value of 5000 i.e., 5 seconds to show that the program will stop its execution and exits the terminal after 5 seconds. So, we added a print statement at line no. 9 to show the working. As it is evident, in the output line no.9 is not printed and the program after printing “The program will exit in 5 seconds” terminates. So, this is delay() function you can try it out please read the Note carefully.

Comment down below if you have any queries related to C++ delay() function.

The post C++ Delay Function with Program Example appeared first on The Crazy Programmer.

0 0 votes
Article Rating