What is the time() function in C?

The time() function is included in the time.h header file in C. The return value is the time (in seconds) since 00:00:00 UTC, January 1, 1970. The return value is stored in the variable second if it is not NULL.

Syntax

time_t time( time_t *second )

Parameters

  • second: Pointer to the time_t object where the time will be stored.

Return value

The time() function returns the current calendar time as type time_t object.

Code example

The code below demonstrates the time() function using the time_t object defined as seconds to store the time. The code outputs the number of seconds, hours, and days since January 1, 1970.

#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
time(&seconds);
printf("Seconds since January 1, 1970 = %ld\n", seconds);
printf("Hours since January 1, 1970 = %ld\n", seconds/3600);
printf("Days since January 1, 1970 = %ld\n", seconds/(3600*24));
return(0);
}

Free Resources