Javatpoint Logo
Javatpoint Logo

usleep() function in C

In this article, we will discuss the usleep() function with its syntax and examples.

In C programming, the "usleep()" function briefly pauses the running program. This "usleep()" function can accommodate this brief period of time. We utilize this "usleep()" function to pause the program for a nanosecond. The program's resumption of execution will follow the provided microsecond. We employ this function when we need to suspend or sleep a program for less than a second. Its value is set in microseconds.

The usleep() function will prevent the calling thread from continuing to run the program until the number of real-time microseconds specified by the argument useconds has passed or the calling thread receives a signal. At that point, it will either call a signal-catching function or end the process. Due to the system's scheduling of other activities, the suspension period may be longer than what was requested. The useconds argument ought to be less than 1,000,000. The call is ineffective if useconds is set to 0.

It is unclear whether usleep() function will return when the SIGALRM signal is scheduled if a SIGALRM signal is generated for the caller process during the execution of the usleep() function, and if the SIGALRM signal is being ignored or stopped from delivery. It is also unclear whether the signal is dropped or left pending after usleep() returns if it is being blocked.

It is unclear whether a SIGALRM signal that is generated for the calling process during the execution of usleep(), other than as a result of a previous call to alarm(), and if it is not being ignored or prevented from delivery, has any effects aside from causing usleep() to return.

The outcomes are undefined if a signal-catching function interrupts usleep() and checks or modifiy the time a SIGALRM signal is scheduled to be created, the action linked to the SIGALRM signal, or whether the SIGALRM signal is prevented from delivery.

The action associated with the SIGALRM signal and the moment at which a SIGALRM signal is planned to be generated are undefined if a signal-catching function interrupts the usleep() function and calls siglongjmp() or longjmp() function to restore an environment saved before the usleep() function call. In addition, it is unclear whether the SIGALRM signal is suppressed until the environment also restores the thread's signal mask.

The level of timer value granularity may be constrained by implementations. For each interval timer, the actual timer value must be rounded up to the next supported value if the requested timer value requires a finer granularity than the implementation offers.

Syntax of the usleep() function in C Language

It has the following syntax:

An unsigned integer of type useconds_t is used. It returns 0 if it is successful and returns -1 if it is unsuccessful. The range of values available is 0 to 999,999 microseconds. The microsecond values are acceptable.


The Usleep() function should return 0 if it was successful; otherwise, it should return -1 and set errno to indicate an error.


Let's take an example to demonstrate the usleep() function in C.


Start of program
[2-second delay]
End of program


For the usleep() function and standard input/output routines, we add the required header files, stdio.h, and unistd.h.

"Start of program" is printed to the console as soon as the program commences. We call usleep(2000000) to add a 2-second (2,000,000 microseconds) delay to the program execution.

The program resumes after the delay and prints "End of program" on the console. Due to system scheduling and other considerations, the actual time of the delay might not be perfect; hence, it is more appropriate for small delays than precise timing. Additionally, the usleep() function is a POSIX standard function and might not be present on all systems. Consider utilizing other platform-specific routines, such as Sleep() on Windows or nanosleep() on POSIX systems, if portability problems arise.



Countdown starting...
Countdown completed!


  • "Countdown starting..." is printed to the console as soon as the program launches. After that, we utilize a for loop to display numbers from 1 to 5.
  • Next, we use the printf() function within the loop to show the current value of the loop variable i.
  • We use usleep(1000000); to add a 1-second delay after each number is displayed. As a result, there is a one-second gap between each presentation of a number.
  • We print "Countdown completed!" to the terminal after the loop is finished.
  • A one-second pause will occur between each number while the program displays the numbers 1 through 5. It is a more straightforward illustration of how to use usleep() to add a delay to the program.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA