Home >>C Time Library Function >C difftime() function

C difftime() function

C difftime() function

The C difftime() function is used to return the difference of seconds between time1 and time2 . These two times are specified in calendar time, which represents the time elapsed since the Epoch. This function is defined in <time.h> header file.

Syntax:
double difftime(time_t time1, time_t time2)

Parameter Values

Parameter Description
time1 − It is the time_t object for end time.
time2 − It is the time_t object for start time.
Here is an example 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 10 seconds...\n");
sleep(10);
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);
}

Output:
Starting of the program...
Sleeping for 10 seconds...
Execution time = 10.000000
Exiting of the program...

No Sidebar ads