C Programming

The usleep 3 Function in C

The “usleep()” function in C programming is used to pause the current program for microseconds. We can put this microsecond in this “usleep()” function. When we want to delay the program for a microsecond, we use this “usleep()” function. The program will resume its execution after the given microsecond. We use this when we have to pause or sleep a program for less than a second. We set its value in microseconds.

In this tutorial, we will explore this concept to enhance your knowledge about the C programming concept. The “usleep()” will be explored here in detail and also, and we will demonstrate examples here for you. Look at the syntax and examples shown below:

Syntax of the usleep Function in C Language

int usleep (useconds_t useconds);

The useconds_t type is an unsigned integer. If effective, it returns 0; otherwise, it returns -1. We can set the value from 0 to 999,999 microseconds. The values in microseconds are acceptable.

Example # 1

In this tutorial, the examples are run on Ubuntu 20.04. The GCC compiler must be installed for Ubuntu 20.04 to perform this. The “usleep()” function will be used in some C code that will be created after GCC has been installed to show how it works. Here, we’re writing some code in the text editor included with Ubuntu 20.04. The code described below is first modified to include the header file, such as “stdio. h”.

Because it contains the details for incorporating the input or output functions in the C code, this header file is utilized. Then, we utilize another header file, “stdlib. h”, which is the standard library in C programming. The stdlib contains functions for memory space, process monitoring, converters, and others. Also, we have the “unistd. h” header file used for accessing the POSIX OS API. After these header files, we have the “main()” function. The data type of this “main()” function is “int”, and we pass two parameters in this “main()” function. We put “argc” for the “int” data type, and for the “char” data type, we set the array named “argv []”.

Now, we print a line on the screen using the “printf” statement. This “printf” statement is used in C programming for printing the given line on the output screen or terminal. Here, we use “sleep” for sleeping or pausing the program for some seconds. We use “4” here, and our program will sleep or pause for “5” seconds and then resume its execution.

After this, we again use the “printf” and put one more line here. This line will render when the program resumes its execution after pausing. Now, we are going to use the “usleep()” function, and in this “usleep()”, we are going to set the time in microseconds. We set “2,000,000” microseconds in this “usleep()” function. So, our program will pause for “2,000,000” microseconds here and then resume its execution. After resuming, it also prints a new line which we added here using the “printf” statement. At the last of the program, we put the “return0” statement.

As we are performing these examples on Ubuntu 20.04, we just need to put some commands for rendering the output on the screen. We put two commands here, and it renders the output after compiling this C program. Here, it prints the first line pauses execution for 5 seconds, then prints the second line, pauses the program for 200,000 microseconds, and then prints the last line.

Example # 2

We utilize the header file “stdio. h”. This header file is used because it contains the information needed to include the input or output functions in the C code. Then, we make use of another header file, “stdlib. h”, which is the C programming language’s standard library. The standard library (stdlib) includes converters, process monitors, memory space monitors, and other utilities. The “unistd. h” header file, which is used to access the POSIX OS API, is also available. The “main()” function comes next after these header files.

There are two parameters passed to this “main()” method, which has an “int” data type. We set the array “argv []” for the “char” data type and “argc” for the “int” data type. Now, we will put two different “printf” statements and place a line inside this “printf”. Both lines will render on the output terminal on execution.

Then, we utilize the “usleep” function here because we want to pause or sleep our code for microseconds. We are using “500,000” microseconds for this “usleep()” function. It will pause our program for “500,000” microseconds, and then we put the following “printf” statement this “usleep()” function. We utilize two different “printf” statements after this “usleep()” function. In the end, we put “return 0” and closed the bracket.

We obtain this given output by utilizing the same commands, which we discussed in our previous example. This output’s first two lines are rendered on the screen. Then the program pauses its execution for 500,000 microseconds, and after 500,000 microseconds, it will resume its execution and print the last two lines.


The primary objective of this tutorial is to provide you with the understating of the “usleep()” as a C programming function. The purpose, codes, and results of the “usleep()” function were covered in this tutorial. We’ve already talked about the C function “usleep()” in this tutorial. As we have explained, the “usleep()” function in C is used to pause the execution of the C program for microseconds. Here, we’ve looked at several examples. The results of each example in this tutorial have been shown, together with the C source codes needed to use the “usleep()” function. After carefully reading this tutorial, we are certain that you will understand this “usleep()” function.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.