Display Digital Clock C Program

By | August 5, 2016

C Program To Display Digital Clock

Learn How To Display Digital Clock in C Programming Language. This digital clock will display the current time with real time updates of time.

This C Program makes use of a System Library File which has its header as time.h. The time.h header file defines Two Macro, Four Variable Types and various other functions for manipulating time and date.

Must Read: System Shutdown C Program For Linux Ubuntu

C Program To Display Digital Clock (Live Clock)

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

int main() 
{
      time_t ts, flag = 1;
      struct tm *ct;
      printf("\n\n\n\tActive Digital Clock\n\t");
      while(1) 
      {
            ts = time(NULL);
            ct = localtime(&ts);
            if(flag || ct -> tm_min % 59 == 0) 
            {
                  if(!flag)
                  printf("\b\b\b\b\b\b");
                  printf("%02d:%02d:%02d", ct -> tm_hour, ct -> tm_min, ct -> tm_sec);
                  flag = 0;
                  printf("\b\b");
                  continue;
            }
            if(ct -> tm_sec % 59 == 0) 
            {
                  printf("\b\b\b");
                  printf("%02d:", ct -> tm_min);
            }
            printf("%02d", ct ->tm_sec);
            printf("\b\b");
      }
      return 0;
}

Must Read: C Program To Print System Date and Time

 

Output

Display Digital Clock in C Programming

If you have any compilation error or doubts in this C Program To Display Active Digital Clock, let us know about it in the Comment Section below.

5 thoughts on “Display Digital Clock C Program

  1. Parmar Suraj

    Thanks. This program code for printing live clock works perfectly fine. Can we actually make a program to display only static time?

    Reply
    1. Tushar Soni Post author

      Yes. There are so many built in methods in C Libraries to display static time and date. You will get it on google.

      Reply

Let's Discuss