HOWTO · C++

How to Make a Countdown Timer in C++

Create portable C++ countdown and up-counting timers with chrono and sleep_for.

On this page

A C++ timer can display a countdown or count upward while pausing between values. For portable elapsed-time handling, use the standard library’s chrono and thread facilities. The example below takes a total duration in seconds; it does not ask for separate minute and second values.

Create a portable countdown timer in C++

std::chrono::steady_clock is a monotonic clock, so it is suitable for measuring elapsed time even if the system clock is adjusted. std::chrono::seconds represents the duration passed to std::this_thread::sleep_for, which asks the current thread to pause. The sleep duration is a request to the operating system, not a promise that the next instruction runs at an exact instant.

Run this example:

#include <chrono>
#include <iostream>
#include <thread>

int main() {
    constexpr int total_seconds = 3;

    for (int remaining = total_seconds; remaining >= 0; --remaining) {
        std::cout << remaining << '\n';
        if (remaining > 0) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    }
}

The loop starts at the configured total, displays each value, and subtracts one until it displays zero. The condition remaining > 0 avoids an unnecessary pause after the final value. Change total_seconds to set the timer length. If the duration comes from input, read one integer representing the total seconds and validate that input before starting the loop.

Use platform-specific sleep functions

Older examples may use operating-system sleep APIs. On Unix or POSIX systems, usleep takes microseconds, and POSIX requires its argument to be less than 1,000,000; for example, usleep(500000) requests a half-second pause. On Windows, Sleep is the Windows API and takes milliseconds, so Sleep(1000) requests one second. Sleep is not named sleep(). These functions are platform-specific; standard C++’s std::this_thread::sleep_for is the portable choice. See the windows.h overview and the guide to getting time in milliseconds for related details.

Create an up-counting timer in C++

An up-counter begins at a chosen value and increments until it reaches a limit. This example displays six values from 5 through 10, pausing between them. The start and limit are independent, so adjust either to fit the task.

#include <chrono>
#include <iostream>
#include <thread>

int main() {
    constexpr int start = 5;
    constexpr int limit = 10;

    for (int value = start; value <= limit; ++value) {
        std::cout << value << '\n';
        if (value < limit) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    }
}

Unlike the countdown, this loop increases its value and stops after displaying the inclusive limit. The pause is skipped after the last display, so the program exits without waiting for another tick.

Understand timing accuracy and limitations

A sleep does not make a timer precise by itself. The operating system may resume a sleeping thread later than requested, and work performed in each iteration adds time before the next pause. For a display that should follow elapsed time, compare the current std::chrono::steady_clock value with a start time and calculate the remaining or elapsed duration; do not assume that counting one sleep call as exactly one second will stay synchronized over a long run. Keep display updates separate from the time calculation when accuracy matters.