当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C语言 difftime()用法及代码示例


C 库函数 difftime() 返回开始时间和结束时间之间的差值,以秒为单位。(结束 time-starting 时间)

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

用法:
double difftime(time_t time2, time_t time1);

参数:
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 <time.h>
#include <stdio.h>
#include <unistd.h>
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

相关用法


注:本文由纯净天空筛选整理自GeeksforGeeks大神的英文原创作品 difftime() C library function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。