Home  >  Article  >  Backend Development  >  How to use the time() function in C?

How to use the time() function in C?

藏色散人
藏色散人Original
2019-03-21 17:37:246871browse

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

How to use the time() function in C?

Syntax:

time_t time( time_t *second )

Parameters: This function accepts a single parameter second. This parameter is used to set the time_t object that stores the time.

Return value: This function returns the current calendar time as an object of type time_t.

Example 1:

#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:

#include <stdio.h> 
#include <time.h> 
   
int main() 
{ 
    time_t seconds; 
   
     // 存储时间秒
    time(&seconds); 
    printf("Seconds since January 1, 1970 = %ld\n", seconds); 
   
    return 0; 
}

Output:

Seconds since January 1, 1970 = 1538123990

Related recommendations: "C Tutorial"

This article is an introduction to the use of the time() function in C. I hope it will help Friends in need help!

The above is the detailed content of How to use the time() function in C?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn