Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

time() function in C

  • Difficulty Level : Basic
  • Last Updated : 15 Jul, 2021

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

Program 1: 

C




// 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);
}


Output: 

Seconds since January 1, 1970 = 1538123990

 

Example 2: 

C




// 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;
}


Output: 

Seconds since January 1, 1970 = 1538123990

 

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!