📜  difftime()C库函数

📅  最后修改于: 2021-05-26 00:04:20             🧑  作者: Mango

C库函数difftime()返回开始时间结束时间之间的差(以秒为单位) (结束时间开始时间)

// It is present in time.h header file
#include 

Syntax : 
double difftime(time_t time2, time_t time1);

Parameters:
time1 : Lower bound of the time interval
        whose length is calculated.
time2 : Higher bound of the time interval
        whose length is calculated.

Return value : 
Returns the difference between time1 and 
time2 (as measured in seconds).
// C program to demonstrate working of
// difftime()
#include 
#include 
#include 
int main()
{
    int sec;
    time_t time1, time2;
  
    // Current time
    time(&time1); 
    for (sec = 1; sec <= 6; sec++) 
        sleep(1);
      
    // time after sleep in loop.
    time(&time2);
    printf("Difference is  %.2f seconds", 
                 difftime(time2, time1));
  
    return 0;
}

输出:

Difference is 6.00 seconds
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。