Home > Backend Development > C#.Net Tutorial > How to use the time() function in C?

How to use the time() function in C?

藏色散人
Release: 2019-03-21 17:37:24
Original
6929 people have browsed it

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 )
Copy after login

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); 
}
Copy after login

Output:

Seconds since January 1, 1970 = 1538123990
Copy after login
Copy after login

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; 
}
Copy after login

Output:

Seconds since January 1, 1970 = 1538123990
Copy after login
Copy after login

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!

Related labels:
c
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template