Pages

Showing posts with label Clock. Show all posts
Showing posts with label Clock. Show all posts

Sunday, August 28, 2011

How to show a Digital Clock on the Screen using C and C++ Program

Digital Clock
The following C and C++ program will show a digital clock on the output screen. The gettime() function is being used to catch current system time and then the time is being shown with the cprintf() function. The gettime() function accepts a structure type variable reference. So, the time structure variable is being used in this program. The delay() function is also being used to hold the program execution till 1 second or 1000ms.

C and C++ program to show an digital clock

#include<stdio.h>
#include<conio.h>
#include<dos.h>

void main()
{

 struct time t;
 do
 {
  clrscr();
  gettime(&t);
  gotoxy(30,10);
  textcolor(2);
  cprintf("Time: %2d:%02d:%02d",t.ti_hour, t.ti_min, t.ti_sec);
  delay(1000);
 }while(!kbhit());

}

Output