Latest web development tutorials

C library functions - difftime ()

C standard library - <time.h> C standard library - <time.h>

description

C library functionsdouble difftime (time_t time1, time_t time2 ) Returns the number of seconds difference between time1andtime2(time1 - time2). The two times are specified in calendar time, it represents the era since the Epoch (Coordinated Universal Time UTC: 1970-01-01 00:00:00) time elapsed.

statement

Here is difftime () function's declaration.

double difftime(time_t time1, time_t time2)

parameter

  • time1 - This is the end time time_t object.
  • time2 - This was stated start time time_t object.

return value

This function returns the number of seconds difference between the two times double precision floating point double value represented by (time2 - time1).

Examples

The following example demonstrates difftime () function is used.

#include <stdio.h>
#include <time.h>

int main ()
{
   time_t start_t, end_t;
   double diff_t;

   printf("程序启动...\n");
   time(&start_t);

   printf("休眠 5 秒...\n");
   sleep(5);

   time(&end_t);
   diff_t = difftime(end_t, start_t);

   printf("执行时间 = %f\n", diff_t);
   printf("程序退出...\n");

   return(0);
}

Let's compile and run the above program, which will result in the following:

程序启动...
休眠 5 秒...
执行时间 = 5.000000
程序退出...

C standard library - <time.h> C standard library - <time.h>