Create a Digital Clock in C++

In this tutorial, we are going to learn how to create a digital clock in C++. We can use time() and localtime() function to get the local time and then print it on the console. See how it can be done.

This digital clock will update the time automatically with an interval of 1 second in run time.

Digital Clock in C++

These are the steps to create a digital clock in C++.

  • call time() function with argument ‘0’ to store total seconds in total_seconds variable.
  • make a call to localtime() function and pass the address of total_seconds as a parameter. This function splits the seconds into hours, minutes and seconds and returns a structure tm* which holds the values of hours, minutes and seconds. To know more about this, read this: Date and Time in C++
  • Get the values of seconds, minutes and hours from the structure variable.
  • Print the values of hours, minutes and seconds as shown in the example program with an interval of 1 sec. Use the system(“CLS”) statement to clear the screen for each interval.
  • The localtime() function returns time in 24-hour format. Convert it into the 12-hour format and print AM or PM accordingly.

Here, you can see the example program.


using namespace std;

int main()
  int sec_prev=0;
    int seconds, minutes, hours;
    string str;
    //storing total seconds
    time_t total_seconds=time(0);
    //getting values of seconds, minutes and hours
    struct tm* ct=localtime(&total_seconds);
    //converting it into 12 hour format
    //printing the result
    if(seconds==sec_prev+1 || (sec_prev==59 && seconds==0))
      cout<< (hours<10?"0":"") << hours <<":" << (minutes<10?"0":"") << minutes << ":" << (seconds<10?"0":"") << seconds << " " << str <<endl;
  return 0;


You may also learn: C++ program to create a stopwatch

The output of the program will be like this and is updated every second:

06:56:07 AM
06:56:08 AM
06:56:09 AM


As explained above, we first call time() function by passing 0 as a parameter which gives us total seconds. The localtime() function converts these seconds into hours, minutes and seconds in 24-hour format. We print the result after converting it to the 12-hour format. If the hour value is greater than 12 we subtract 12 from it otherwise we print it as it is. If the hour value is 12 or greater than 12, we add PM in the result otherwise we add AM. We have used a sec_prev variable to store the value of seconds of the time value obtained from the previous execution of the while loop so that we can compare it to the current value of seconds and when the difference between them is 1 sec, we can clear the screen using system(“CLS”) and print the result in required format.

Also read: Ternary Operator in C++

5 responses to “Create a Digital Clock in C++”

  1. saad asi says:

    can you give me explanation about this program and also tell about that:
    why we are using ? in this line
    if(seconds==sec_prev+1 || (sec_prev==59 && seconds==0))
    cout<< (hours<10?"0":"") << hours <<":" << (minutes<10?"0":"") << minutes << ":" << (seconds<10?"0":"") << seconds << " " << str <<endl;
    and explain these line also…….

  2. Ranjeet V says:

    ‘?’ is used with ‘:’ as ternary operator.
    The syntax of this operator is like this:
    (condition? statement 1: statement 2;)
    if the condition is true, then statement 1 is executed. If the condition is false, then statement 2 is executed.
    For example, in (hours < 10 ? "0" : "") if (hours < 10) is true then 0("0") is printed otherwise nothing ("") is printed. Same is the case with (minutes < 10 ? "0" : "") and (seconds < 10 ? "0" : "").
    All this is done to make sure if hours(or minutes or seconds) is less than 10 i.e. 6, 7, 8, etc, it's printed as 06, 07, 08.

  3. Raoul Brian B Meva says:

    Why is the condition for the while statement “1”? What does it represent?

  4. Muhammad shuaib says:

    its amazing everything thanks its good idea of programing

Leave a Reply

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