Skip to content
Related Articles
Open in App
Not now

Related Articles

difftime() Library Function in C

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 04 Jan, 2022
Improve Article
Save Article

difftime() is a C Library function. It returns the difference in time, in seconds(i.e. ending time – starting time). It is present in the <time.h> header file. 


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 <stdio.h>
#include <time.h>
#include <unistd.h>
// Driver Code
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

Exception in difftime():

It never throws an exception.

This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!