當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。