What Is sleep() function and How To Use It In C Program?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/what-is-sleep-function-and-how-to-use-it-in-c-program/
PINTEREST
PINTEREST
INSTAGRAM

C programming language provides slepp() function in order to wait current thread for specified time. slepp() function will sleep given thread specified time for the current executable. Of course the CPU and other processes will run without problem.

Include unistd.h Library In Linux

sleep() function is provided by unistd.h library which is short cut of unix standard library. We can include this library like below.

Include windows.h Library In Windows

If we are writing an application which will run on windows platform we should include windows.h library like below.

Cross Platform Library Solution

Actually there is no cross plat format default library. But we can write our application which will use proper library according to compile architecture. In this case we will use _WIN32 constant which will put appropriate library accordingly.

Sleep Example App

In this part we will use sleep() function in an example . In this exmaple we want to sleep for 1 second. As we can see the parameter will be 1 which is an integer.

Sleep For 10 Second

We can also sleep for 10 seconds without a problem. We wil just provide the 10 to the sleep function like below.

Sleep For 100 millisecond

As stated previously the sleep function will interpret given value as second. What if we need to sleep in miliseconds which is lower than second. We can use decimal or float values. In this example we will sleep for 10 milisecond which can be expressed like 0.01 or 0.010

Sleep Millisecond with usleep() Function

We can also use usleep() function which will sleep given value in mili second. In this case we will sleep for 10 mili second with usleep() function.

LEARN MORE  Compiling C and C++ Applications with GCC

Return Value

The sleep() function will return void which means it will not return anything.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/what-is-sleep-function-and-how-to-use-it-in-c-program/
PINTEREST
PINTEREST
INSTAGRAM