C Programming

Utime() 2 C Function

The utime() function is used for setting the modification time for the file or directory that is identified by the path. The accessing and modification times of the file or directory are adjusted to the present time if the time’s parameter is NULL. The process’ effective user ID must reflect the user of the file or directory. The processes should have a written permission to the file or directory, or the program must have the required permissions in order to utilize the utime() function in this way. The accessing and alteration times of the file or directory are adjusted to the values found in the actime and modtime fields of the chosen structure if the times argument is not NULL. If not, it is considered as a pointer toward a utimbuf structure.

The utime() function can only be used by the programs with the right privileges, file, or directory owner. In this tutorial, we will represent how to use this “utime()” function in C language by demonstrating the different examples.

Syntax of the Utime Function in C Language

#include <utime.h>

Int utime (const char * path, const struct utimbuf * times);

Here, in this syntax, we have two different parameters for the utime() function:

  • The “path” parameter is a null-terminated path identifier pointer to the file whose timings should be updated.
  • The “times” parameter is a pointer to the utimbuf structure which has the times that need to be updated.

Example 1:

We preform these given instances in Ubuntu 20.04. For this, we must install the gcc in our Ubuntu 20.04. After installing, we write some code in which we utilize this “utime()” function in our C program and you will easily learn how it works. In the code, which is given in the following image, we first add some header files such as “<stdio.h>” which is used for printing on the screen, and the “<utime>” for setting the updated time for the file or directory identified by the path.

After this, we put the “main” function of the “int” data type. Inside this, we declare an “int” variable with the name “argc” and a “char” array with the name “argv[]”. Now, we utilize the “if” statement here in which we put the “if” condition. Inside this “if” condition, we put the “utime ()” function by following the syntax of this function. The “utime” function’s parameters are “arg[1], Null”, where “arg[1]” represents the “path” and “NULL” represents the “times” parameter which is not equal to zero and the “argc” value is greater than “1”. Then, print the line which we write in the “printf” statement and print the “argv[1]” value with this. Otherwise, if the condition is false, ignore the statement which is written inside the “if” curly braces and print the statement which is given outside the “if” curly braces.

After this, we have the “return” statement. Now, when this code is completed, we must save our code with the “.c” file extension.

We can get the output of the previous code by typing some commands in the terminal as shown here. In the output, it prints the statement which is written outside the “if” statement. So, it means that the “if” condition is not true.

Example 2:

In our next example, we put some header files which is necessary for this code. Then, we call the “main” function which is of the integer data type. We declare the “int” with the name “fd” and declare a “char” array of the name “fn[]” and initialize it with “utimes.c”. We have a “struct” with the name “utimbuf” which is used for specifying a new access and modification time for a file.

After this, we have the “if-else” condition in which we put the condition. If this condition is true, it executes the statement which is given below where we call the “perror” function. This function displays the description of the error. If the given condition is false, the else part is executed where we put the “close( )” function. After this, we have a “puts” function which is used for rendering the line on the output screen.

The “system()” function is used here for passing the commands which are executed in the command processor and gives the command after its completion. We initialize the “ubuf.modtime” with “0” and then use the “time()” function. This function is used for returning the calendar time. After all these, we again utilize the “if-else” condition. This time, we put the different conditions in the “if” statement. In the “if” condition, we place the “utime()” function and pass the “fn” and “&ubuf” as the parameters of the “utime()” function.

Here, the condition is that if the “utime (fn, &ubuf)” function is not equal to “0”, execute the “perror” function. If this condition is false, it executes the else statements where we have the “puts” and “system” statements. After this, we have an “unlink” function outside the “if-else” condition. This “unlink()” function removes the file name if all the overhead situations are false.

Here, we have the output of the previous code which renders the time before the “utime()” function and then after the “utime()” function.


We presented this tutorial to help you understand the concept of the “utime()” function in the C programming. We explored this concept in deep detail and demonstrated the different examples where we utilized the “utime()” function. We discussed that this “utime()” function is used for setting the modification time for the file or directory that is identified by the path. I hope that after carefully reading this guide, we helped enhance your knowledge about the concepts of the C programming.

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.