C program to print digital clock

C program to print digital clock

This program is written in C Language using graphics. This will show day, month, date, current time and year as output.

digital clock in c
digital clock in c


Logic for the digital clock program

  • Initialize hour, minute, seconds with 0.
  • Run an infinite loop.
  • Increase second and check if it is equal to 60 then increase minute and reset second to 0.
  • Increase minute and check if it is equal to 60 then increase hour and reset minute to 0.
  • Increase hour and check if it is equal to 24 then reset hour to 0.

Let’s see the source code of the program

#include <graphics.h>
#include <time.h>

// driver code
int main()
// DETECT is a macro defined in
// “graphics.h” header file
int dt = DETECT, gmode, midx, midy;
long current_time;
char strtime[30];

// initialize graphic mode
initgraph(&dt, &gmode, “”);

// to find mid value in horizontal
// and vertical axis
midx = getmaxx() / 2;
midy = getmaxy() / 2;

// set current colour to white

// make a rectangular box in
// the middle of screen
rectangle(midx – 200, midy – 40, midx + 200,
midy + 40);

// fill rectangle with white color
floodfill(midx, midy, WHITE);
while (!kbhit()) {

// get current time in seconds
current_time = time(NULL);

// store time in string
strcpy(strtime, ctime(&current_time));

// set color of text to red

// set the text justification
settextjustify(CENTER_TEXT, CENTER_TEXT);

// to set styling to text
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 3);

// locate position to write
moveto(midx, midy);

// print current time

// deallocate memory for graph

Leave a Reply