C library function - difftime()

Advertisements


Description

The C library function double difftime(time_t time1, time_t time2) returns the difference of seconds between time1 and time2 i.e. (time1 - time2). The two times are specified in calendar time, which represents the time elapsed since the Epoch (00:00:00 on January 1, 1970, Coordinated Universal Time (UTC)).

Declaration

Following is the declaration for difftime() function.

double difftime(time_t time1, time_t time2)

Parameters

  • time1 -- This is time_t object for end time.

  • time2 -- This is time_t object for start time.

Return Value

This function returns the difference of two times (time2 - time1) as a double value.

Example

The following example shows the usage of difftime() function.

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

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

   printf("Starting of the program...\n");
   time(&start_t);

   printf("Sleeping for 5 seconds...\n");
   sleep(5);

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

   printf("Execution time = %f\n", diff_t);
   printf("Exiting of the program...\n");

   return(0);
}

Let us compile and run the above program, this will produce the following result:

Starting of the program...
Sleeping for 5 seconds...
Execution time = 5.000000
Exiting of the program...


Advertisements
Advertisements