time() function in C

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

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 calender time as a object of type time_t.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate
// example of time() function.
#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);
}

chevron_right


Output:

Seconds since January 1, 1970 = 1538123990

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate
// example of time() function.
   
#include <stdio.h>
#include <time.h>
   
int main()
{
    time_t seconds;
   
     // Stores time seconds
    time(&seconds);
    printf("Seconds since January 1, 1970 = %ld\n", seconds);
   
    return 0;
}

chevron_right


Output:

Seconds since January 1, 1970 = 1538123990


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.