- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <float.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdarg.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <time.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library function - difftime()
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 the time_t object for end time.
time2 − This is the time_t object for start time.
Return Value
This function returns the difference of two times (time1 - time2) 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 that will produce the following result −
Starting of the program... Sleeping for 5 seconds... Execution time = 5.000000 Exiting of the program...
time_h.htm
Advertisements