C Standard Library

C <time.h> - clock_t Type



The C <time.h> clock_t type is an alias of a fundamental arithmetic type capable of representing clock tick counts. Clock ticks are units of time of a constant but system-specific length, as those returned by clock() function.

In the <time.h> header file, it is defined as follows:

typedef /* unspecified */ clock_t;              

Example:

The example below shows the usage of clock_t type.

#include <stdio.h>
#include <time.h>
 
int main (){
  clock_t start, finish;
  long product;

  start = clock();
  for(int i = 0; i < 100000; i++)
    for(int j = 0; j < 25000; j++) 
      product = i*j;

  finish = clock();

  //calculating the time difference 
  //in ticks and in milliseconds
  printf("Time taken = %ld ticks (%lf milliseconds)",
        (finish - start), 1000.0 * (finish - start)/CLOCKS_PER_SEC);   
  return 0;
}

The output of the above code will be:

Time taken = 6334701 ticks (6334.701000 milliseconds)

❮ C <time.h> Library