The time() function is a standard library function defined in the <time.h> header file in C and <ctime> in C++. It returns the current calendar time as the number of seconds elapsed since 00:00:00 UTC, January 1, 1970 (Unix Epoch).
- Returns the current time as a Unix timestamp in seconds.
- Can also store the returned time in a user-provided time_t object if a valid pointer is passed.
#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
seconds = time(NULL);
printf("Seconds since January 1, 1970 = %ld\n", seconds);
return(0);
}
Output
Seconds since January 1, 1970 = 1538123990
Syntax
time_t time( time_t *second )
Parameter: This function accepts single parameter second. This parameter is used to set the time_t object which store the time.
Return Value: This function returns current calendar time as a object of type time_t.
Advantages
The time() function provides a simple and portable way to obtain the current system time for various time-related operations.
- Returns the current calendar time as a Unix timestamp in seconds.
- Can directly store the current time in a
time_tobject through the function parameter. - Widely supported across C and C++ compilers, making it portable.
Limitations
Although time() is useful for obtaining the current time, it has some limitations.
- Returns time only in seconds, so it cannot measure milliseconds or microseconds.
- Depends on the system clock, so incorrect system time affects the returned value.
- Suitable for general timekeeping but not for high-precision performance measurements.

