Open In App
Related Articles

C Library Function – difftime()

Improve Article
Save Article
Like Article

The difftime() is a C Library function that returns the difference in time, in seconds(i.e. ending time – starting time). It takes two parameters of type time_t and computes the time difference in seconds. The difftime() function is defined inside the <time.h> header file.


The syntax of difftime() function is as follows:

double difftime(time_t time2, time_t time1);


The difftime() function takes two parameters:

  • time1: Lower bound of the time interval whose length is calculated.
  • time2: Higher bound of the time interval whose length is calculated.

where time1 and time2 are variables of type time_t which is a predefined structure for calendar times.

Return Value

  • Returns the difference between time1 and time2 (as measured in seconds).

Example of difftime() in C


// 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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 May, 2023
Like Article
Save Article
Similar Reads