difftime() C library function

The C library function difftime() returns the difference, in seconds between starting time and ending time.(ending time-starting time)

// It is present in time.h header file

Syntax : 
double difftime(time_t time2, time_t time1);

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
    for (sec = 1; sec <= 6; sec++) 
    // time after sleep in loop.
    printf("Difference is  %.2f seconds"
                 difftime(time2, time1));
    return 0;



Difference is 6.00 seconds

This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.