Introduction

A digital clock is a timekeeping device that displays the current time in digits, usually on an LCD or LED screen. It shows hours, minutes, and seconds in real-time, offering a simple and user-friendly way to track the time. In this tutorial, we will write a simple C++ program that continuously displays the current time on the console, simulating a digital clock.

Objective

The objective of this program is to create a simple digital clock using C++ that updates every second to display the current time. This program uses built-in C++ libraries to fetch and display the time continuously, demonstrating basic concepts of C++ programming such as loops and time manipulation.

Program Code

#include 
#include 
#include 
#include 
#include 

using namespace std;

void displayClock() {
    while (true) {
        // Get the current time
        time_t currentTime = time(0);
        struct tm* localTime = localtime(&currentTime);

        // Clear the screen (for better view in console)
        system("CLS");

        // Display the current time
        cout << setw(2) << setfill('0') << localTime->tm_hour << ":";
        cout << setw(2) << setfill('0') << localTime->tm_min << ":";
        cout << setw(2) << setfill('0') << localTime->tm_sec << endl;

        // Sleep for 1 second before refreshing the clock
        this_thread::sleep_for(chrono::seconds(1));
    }
}

int main() {
    cout << "Digital Clock Program\n";
    displayClock();
    return 0;
}

Explanation of the Program Structure

The program consists of a main function and a displayClock function. Here’s how the program works:

  • Header Files: The program uses the <iostream>, <iomanip>, <ctime>, <thread>, and <chrono> header files. These libraries provide the necessary functions to handle input/output operations, time handling, and to introduce delays in the program.
  • displayClock Function: This function runs in an infinite loop and continuously fetches the current system time using the time(0) function, which returns the current time. It then formats and prints the hours, minutes, and seconds in a digital clock format. The system("CLS") command is used to clear the console screen for better readability.
  • Time Formatting: The setw(2) and setfill('0') functions from the <iomanip> library are used to ensure that the hours, minutes, and seconds are always displayed as two digits (e.g., 08:09:01).
  • Sleep Mechanism: The this_thread::sleep_for(chrono::seconds(1)) command pauses the program for 1 second before refreshing the displayed time.
  • Main Function: The main function simply starts the displayClock function to begin the clock’s execution.

How to Run the Program

To run this program, follow these steps:

  1. Write the code in a C++ IDE or text editor (e.g., Visual Studio, Code::Blocks, or Notepad++).
  2. Save the file with a .cpp extension (e.g., digital_clock.cpp).
  3. Compile the program using a C++ compiler (e.g., g++). For example: g++ digital_clock.cpp -o digital_clock
  4. Run the compiled program in your terminal or command prompt: ./digital_clock.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)