Share on Google+
A Clock developped in C
Below is the program in C to build a beautiful clock synchronized with current time of your system. This program uses some basic understanding of C and graphics in C. You can design such animations or graphic design very easily. I would like to make you understand this directly through this program.
Download the source code here.       
Take a C quiz now


If you like this article, please
about below topic

Above are the inclusion of header files for this program. Header files contains functions and definitions which will be used below. For Eg, to use gettime funtion, we need to include <TIME.h>
void main()
int gd=DETECT,gm;
struct time t;
 //t is structure of type time.
 //members:- ti_min,ti_hour,ti_sec
double x1,y1,x2,y2,x3,y3,r=220.0,r1=160.0,r2=130.0,pi=3.1416,i,j,k;
double cx,cy,secangle,minangle,hrangle; //co-ordinates of center
int a[8]={0,0,0,600,600,600,600,0};
float min,hr,sec;
//variable declarations and initializations
// initializes the graphic mode. If not used, it gives error graphics not initialized.
cx=getmaxx()/2;        //(cx,cy) is the co-ordinate of center
  //getmaxx() will get the maximum value of pixel in X-direction.
//getmaxy() will get the maximum value of pixel in Y-direction
//This function gets current system time ans stores into the structure time t(declared above)
//storing current hrs, mins, seconds into other variables (just for access with ease)
//calculate the displacement of all hands of a clock measured in angle.
//angle increment for second hand movement=pi/30
//angle increment for minute hand movement=pi/1800
//angle increment for hour hand movement=pi/21600
//calculate curent angle for second hand
if (secangle <0)
//conversion from degree to radian as sin cos in C accepts arguments in radian

minangle=90-(min*6); //calculate curent angle for minute hand
if (minangle <0)
minangle=(minangle*pi)/180;  //conversion from degree to radian
//same for hour hand
hr=hr+(min/60.0);  //adding minutes also, to the hour value.
if (hr>12)
     hr-=12;  // gettime returns hours in 24 hour formate, so subtract by 12.
if (hrangle<0)
/*setting initial position(angles) of all hands. i.e setting i,j,k with secangle, minangle and hrangle respectively . Note how the angle incremet is set in each second. For second hand the angle will change by pi/30 in one second and so on..
 delay(1000);       //delay for 1 seconds and below code updates the position of clock's hands
 cleardevice();        //erases all and clears the graphic screen
 setcolor(13);     // sets the current drawing color using the value (here 13).
 fillpoly(4,a); //draws an fills a polygon with the current style and drawing color
     // gotoxy(300,100);
      // printf("34");
 line(cx,cy-r-10,cx,cy-r+10); //extra lines to
 line(cx,cy+r-10,cx,cy+r+10); // show 12,3,6,9
 line(cx,cy,cx+r*cos(i),cy-r*sin(i));          //second hand
 line(cx,cy,cx+r1*cos(j),cy-r1*sin(j));          //minute hand
 line(cx,cy,cx+r2*cos(k),cy-r2*sin(k));          //hour hand
 if(kbhit()!=0) //checks if a key is pressed
 break; // if a key is pressed, exits...

 }//end of kbhit();