个性化阅读
专注于IT技术分析

如何使用C库函数difftime()?代码示例

点击下载

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

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

赞(0)
未经允许不得转载:srcmini » 如何使用C库函数difftime()?代码示例

评论 抢沙发

评论前必须登录!