delay() and sleep() functions of dos.h in C

In this article, we are going to learn about the process of terminating functions such as delay() and sleep() of dos.h header file in C.
Submitted by Manu Jemini, on March 15, 2018

Need of delay() and sleep() functions

What to do when you want to delay an action in your program or make your program sleep for a particular time?

The Solution is very simple and straightforward, all we need to do is, use the delay() and sleep() function from the dos.h file.

<dos.h> delay() and sleep() functions

delay() function requires a parameter which should be a number, defining the milliseconds for the delay. sleep() function requires a parameter which should be a number, defining the seconds to sleep.

These functions are pretty useful when you want to show the user multiple outputs, for a given period of time. The nice thing about this is that we can also make alarm and reminder for the user in our program. Hence, these two functions are pretty handy, if you are planning to make a real-world application.

Examples of <dos.h> delay() and sleep() functions

dos.h - delay() function Example in C

#include <stdio.h>
// to use 'delay()'
#include <dos.h>

int main() {
  // message for user
  printf(
      "After printing this message the program will get end in next 5 seconds "
      "\n");

  // delay the process for 5 seconds as it takes integer value in milliseconds.
  delay(5000);

  return 0;
}

Output

dos.h - delay() function Example in C

dos.h - sleep() function Example in C

#include <stdio.h>
// to use 'sleep()
#include <dos.h>

int main() {
  // message for user.
  printf(
      "After printing this message the program will get delay for next 15 "
      "seconds\n");

  // to terminate the process for next 15 seconds.
  sleep(15);

  return 0;
}

Output

dos.h - sleep() function Example in C



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.