C clock() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

The C clock() function reports an implementation-defined count related to processor time consumed by the program. It is useful for measuring CPU work, not for reading the current date or wall-clock time.

Subtract two clock_t readings and divide by CLOCKS_PER_SEC to obtain seconds as a floating-point value. On failure, the function returns (clock_t)-1. Include <time.h>.

Syntax

Start with this concrete clock() call:

C
clock();
CallWhat it does
clock();Reads the program's current processor-time counter.

Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.

Library declaration
C
clock_t clock(void);

Parameters

ParameterDescription
Noneclock() takes no arguments.

Return Value

Return ValueDescription
clock_t countCurrent processor-time count for the program.
(clock_t)-1Processor time is unavailable or cannot be represented.

C Examples

Example 1 — Measure a Loop

This example demonstrates clock() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    clock_t start = clock();
    volatile long total = 0;
    for (long i = 0; i < 1000000; ++i) total += i;
    clock_t finish = clock();
    double seconds = (double)(finish - start) / CLOCKS_PER_SEC;
    printf("CPU seconds: %.6f\n", seconds);
    return 0;
}

How It Works

  1. Step 1. Read the processor counter before the loop.
  2. Step 2. Run a small CPU-bound calculation.
  3. Step 3. Subtract the first count from the second.
  4. Step 4. Divide by CLOCKS_PER_SEC; the exact result depends on the machine.

Example 2 — Check for Failure

This example demonstrates clock() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    clock_t value = clock();
    if (value == (clock_t)-1) {
        printf("CPU time unavailable\n");
    } else {
        printf("clock ticks: %lld\n", (long long)value);
    }
    return 0;
}

How It Works

  1. Step 1. Call clock() once.
  2. Step 2. Compare with the documented failure sentinel.
  3. Step 3. Convert to long long only for this simple display.
  4. Step 4. A successful reading is an implementation-defined tick count, not seconds yet.

Example 3 — Compare Two Workloads

This example demonstrates clock() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    clock_t start = clock();
    for (volatile long i = 0; i < 500000; ++i) { }
    clock_t middle = clock();
    for (volatile long i = 0; i < 1000000; ++i) { }
    clock_t end = clock();
    printf("first: %.4f s\n", (double)(middle-start)/CLOCKS_PER_SEC);
    printf("second: %.4f s\n", (double)(end-middle)/CLOCKS_PER_SEC);
    return 0;
}

How It Works

  1. Step 1. Capture readings around each separate loop.
  2. Step 2. The first subtraction covers only the first workload.
  3. Step 3. The second subtraction covers only the second workload.
  4. Step 4. Convert both intervals with the same CLOCKS_PER_SEC constant.

Frequently Asked Questions

clock() measures processor time used by the program.
Include <time.h> before calling clock().
On success it returns the processor-time count. On failure it returns (clock_t)-1.
clock() is not a stopwatch for elapsed real time. Sleeping or waiting for input may add little or no processor time.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: ctime()

Continue through the C time.h function tutorials.

ctime() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful