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!