C time()

The time() function is defined in the time.h header file. It is a utility function and it helps to measure the elapsed time. This function returns when the Epoch (00:00:00 UTC, January 1, 1970) is measured in seconds.


time_t time(time_t *second); #where second should be a pointer

 

time() Parameters: 

The time() function takes a single parameter 'seconds' it helps to set the time_t object to store the time.

Parameter Description Required / Optional
seconds  pointer to an object of type time_t, where the second's value will be stored Required

time() Return Value

The function returns the current calendar time as a time_t object. If the parameter seconds is a NULL pointer the return value is not stored anywhere otherwise, the return value stored in the object pointed to by the second.

Input Return Value
if parameter current calendar time

Examples of time() 

Example 1: Working of time() function in C?


#include <stdio.h>
#include <time.h>

int main()
{
   time_t sec;

   sec = time(NULL);
   printf("The hours since January 1, 1970 = %ld\n", sec/3600);
  
   return(0);

}

Output:


The hours since January 1, 1970 = 393923

Example 2: How time() works in C?


#include <stdio.h>
#include <time.h>

int main (){

   time_t secnd;
      
    secnd = time(NULL);
    printf("The seconds since January 1, 1970 = %ld\n", secnd);
      
    return(0);
}

Output:


The seconds since January 1, 1970 = 1538123990