Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax:
3.
Example
4.
Example 
5.
Frequently Asked Questions
5.1.
Which header file is used for time() in c?
5.2.
time() function return time since which date?
5.3.
In which time format does the time() function return?
5.4.
How many parameters are required in the time() function?
5.5.
What happens when the time_t pointer is not NULL?
6.
Conclusion
Last Updated: Mar 27, 2024

time() Function in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction


The time() method is defined in the time.h (ctime in C++) header file. This method returns the time in seconds since 00:00:00 UTC, January 1, 1970 (Unix timestamp). If the second is not a null pointer, the returned value is also saved in the object referred to by the second.

Also see: C Static Function and  Short int in C Programming

Syntax:

time_t time( time_t *second )


Parameter: This function only accepts one parameter, second. This option is used to initialize the time t object, which is used to hold the time.
Return Value: This method returns the current calendar time as a time_t object.

Let’s check the first case, which is when the time_t pointer is NULL.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example

#include <stdio.h>
#include <time.h>
  
int main ()
{
    time_t seconds;
    seconds = time(NULL);
     printf("Seconds since January 1, 1970 = %ld", seconds);
    return(0);
}

Output:

Seconds since January 1, 1970 = 1538123990

Now, let’s go to the second case when the time_t is not NULL.

Example 

In this case, since the return value will be stored to the pointer location, there is no need to assign it separately!

#include <stdio.h>
#include <time.h>
 
int main ()
{
   time_t seconds;
   time(&time_t);
    printf("Seconds since January 1, 1970 = %ld", seconds);
   return(0);
}

Output:

Seconds since January 1, 1970 = 1538123990

You can also read about C dynamic array.

Frequently Asked Questions

Which header file is used for time() in c?

time() function is defined in the "time.h” header file, which is included for using the function to get the elapsed time.

time() function return time since which date?

time() function returns the time elapsed since 00:00:00 UTC, January 1, 1970. The returned time is in the second format.

In which time format does the time() function return?

time() function return time in seconds format. You can change it by converting it into a different format.

How many parameters are required in the time() function?

Only one parameter is required in the time() function; it can be null or a time_t pointer. Depending on the parameters, the workings of time() function are different.

What happens when the time_t pointer is not NULL?

If time_t pointer is not NULL, then the return value gets stored in the memory pointed to by time_t pointer.

Conclusion

In this article, we had a look at time() function in C and also learned its operation.

We hope that this blog has helped you enhance your knowledge of the time() function, and if you would like to learn more about Today and Now functions and Days, Months, and Years functions, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

 

Previous article
tolower() function in C
Next article
User Defined Functions in C
Live masterclass