clock_t - Linux


clock_t is a data type used to represent time measurements in Linux. It is implemented as a signed 32-bit or 64-bit integer, depending on the system architecture.


#include <time.h>


  • None


  • Measure elapsed time:
#include <time.h>

int main() {
    clock_t start = clock();
    // Perform some operation
    clock_t end = clock();
    double elapsed_time = (double)(end - start) / CLOCKS_PER_SEC;
    printf("Elapsed time: %f seconds", elapsed_time);
    return 0;
  • Time certain code block:
#include <time.h>
#include <stdio.h>

int main() {
    clock_t clock_start = clock();
    int i = 0;
    for (i = 0; i < 100000000; i++) {
    clock_t clock_end = clock();
    double time_spent = (double)(clock_end - clock_start) / CLOCKS_PER_SEC;
    printf("Time spent: %f seconds", time_spent);
    return 0;

Common Issues

  • Ensure CLOCKS_PER_SEC is correctly defined on your system.
  • Avoid using clock_t directly for time comparisons, as it can overflow on long-running programs. Use clock_gettime() instead.


  • Combine with clock_gettime() for more precise time measurements.
  • Use with sleep() to pause execution for a specified duration.

Related Commands

  • time: Execute a command while reporting time taken.
  • date: Display or set the system date and time.
  • sleep: Suspend execution for a specified period.