C Programming

Time 2 C Function

The time() function is used for returning the time in the C language. It returns the time in seconds. This time is also entitled “Unix Timestamp”. To utilize this time() function in C language, we must put the header file “time. h”. This function is defined in this “time. h” header file. So, we must put this header file in our C language codes. We can get the time by using this function from the mentioned time to the current time in seconds. In this tutorial, we will explain the “time()” in C programming in detail and will provide examples here.

Syntax of the Time Function in C Language

time_t time ( time_t *second )

The time() function only takes one parameter – “second”. The “time _t” object is used here which stores the time in seconds.

We also have a “return value” which is used to return the current calendar time as an object of type time t.

Example 1:

These examples are run under the Ubuntu 20.04. Therefore, to do this, we must install the GCC in Ubuntu 20.04. After installing it, we create some code using the “time()” function in our C programming so you can quickly grasp how it operates. We first add the several header files such as “<stdio. h>” which is used for printing on the screen to the following code that is provided.

Then, we put the “time. h” header file in which the “time()” function is defined. We can access the “time()” function only when we put this “time. h” header file. After this, we call the “main()” function which is present as the “int” data type. Inside the “main()” function, we put the “time_t” type variable with the name “seconds”. When the return type function returns a value, it is stored in this variable.

For the “seconds” variable, we put the “time()” function and pass the “NULL” as the parameter of this function. Since we want to return the time in the “seconds” variable, we put the “NULL” in the “time()” function. It returns the time in the form of seconds. After this, we utilize the “printf()” function which renders the data written inside the “printf”. In the “printf” statement, “ld” represents the “long integer” because the return value is larger, so we put this “ld”. It returns the time from January 14, 199 to the current time in seconds.

Now, we utilize the “return(0)” at the end of this code. After completing this code, we save this code with the “.c” file extension.

We can easily get the output by utilizing the “gcc” commands. These commands are demonstrated in the following screenshot. In this case, it returns the seconds between the time on January 14, 1999 and the present time.

Example 2:

We modify the previous instance a little bit. The same header files are used in this case, followed by the “main()” function. After this, we create a variable of the “time_t” data type. Then, we put the “time(&seconds)” which is used here for storing the time in seconds. After this, we put the “printf” function and the “return” function.

Following is the output of this code, the time returns in seconds from the mentioned time to the current time. We get this output by utilizing the “gcc” commands.

Example 3:

In this code, we print the time in hours. All the codes are the same as the previous codes. Here, we update the “printf” statement and print the time in hours by dividing the seconds by “3600”. It returns the time from the mentioned date time to the current time.

The output renders the time from January 14, 1999 to the current time in hours. We get the time in hours by dividing the seconds by “3600” in the C program.

Example 4:

We have two header files here and the “main()” function. Declare the “seconds” variable. The data type of this variable is “time_t”. Then, to get the time in seconds, we put the “time(seconds)”. Now, we convert this time which is in seconds into days and print the days from the mentioned date to the current date.

For this, we divide the seconds by “(3600*24)” in the “printf”, so it returns the days from April 10, 1998 to the current date. To return the hours from April 10, 1998 to the current date hours, we just simply divide the seconds by “3600” inside the “printf” statement. Here, the time returns in hours. After this, we also render the time in seconds using the same “printf” statement. We just put “seconds” here in this “printf” statement because we want to print the time in seconds.

Looking at the following output, you can see that it returns the days from April 10, 1998 to the current day, thehours, and theseconds from April 10, 1998 to the current time.

Conclusion

This tutorial’s main goal is to teach you how to use the “time()” function in C programming. In this tutorial, we examined the “time()” function and what it does, why we use it, how to use it, and what output it produces. The “time()” function has already been covered in this tutorial. We discussed that the “time()” function is used to return the time in C programming in seconds. We explored the multiple examples here. The results of each example in this tutorial are shown, along with the codes on how to use the “time()” function in C programming. I believe you will have a solid understanding of the “time()” function after carefully reading this tutorial.

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.