**Also Read: C/C++ Program to Create a Digital Stopwatch**

**Also Read: Simple program to create a circular loading bar using graphics**

**clockLayout()**

I’ve used this function to print the clock layout i.e. clock dial and the markings on the clock. If we observe clearly, the clock has hours marking each separated by 30 degrees and each hour is divided into 5 markings each making an angle of 6 degrees. So, iterating the markings for every 30 degrees gives hours and iterating markings with 6 degrees give minutes markings on the clock. The clock would look like this after executing this function.

**secHand()**

It is clear from the name that this gonna do something with the seconds hand. This function is going to get the present second from the system clock and incline the line according to a particular angle. Eg: if the present seconds is 5 then the angle of the seconds hand with respect to the vertical must be 30 degrees, i.e. 5*6=30.

**minHand()**

This function fulfills the task of moving the minutes hand based on the system clock. The minutes hand must be inclined 6 degrees for every minute passing. Eg: if the elapsed minutes are 30 then the minutes hand angle must be making 180 degrees with the vertical.

**hrHand()**

This function is going to print an inclined hours line. The function is designed to get the present hour and also the no. of elapsed minutes from the system clock and incline the line according to a particular angle. For every hour elapsed the hour hand moves 30 degrees and every 12 minutes it moves 6 degrees.

**main()**

The first lines in main are graphic initialization, you must change the path “c:\turboc3\bgi\” to your compiler’s BGI file path otherwise program will not work. Coming to the while loop, the while loop iterates for every 100 milliseconds reprinting all the functions. This program is like getting the static picture of clock every second and combining all the pictures to make a moving analog clock.

**Also Read: Simple program to create a moving car in graphics**

## Program for Analog Clock in C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
/* Graphical Analog Clock designed in C*/ /*Note press ctrl+pause break to stop the clock while executing in TC*/ #include<stdio.h> #include<graphics.h> #include<stdlib.h> #include<math.h> #include<dos.h> #include<time.h> #define PI 3.147 void clockLayout(); void secHand(); void hrHand(); void minHand(); int maxx,maxy; void main() { int gdriver=DETECT,gmode,error; initgraph(&gdriver,&gmode,"c:\turboc3\bgi\"); error=graphresult(); if(error!=grOk) { printf("Error in graphics, code= %d",grapherrormsg(error)); exit(0); } while(1) { clockLayout(); secHand(); minHand(); hrHand(); sleep(1); /* pausing the outputscreen for 1 sec */ cleardevice(); /* clearing the previous picture of clock */ } } void clockLayout() { int i,x,y,r; float j; maxx=getmaxx(); maxy=getmaxy(); for(i=1;i<5;i++) { /* printing a round ring with outer radius of 5 pixel */ setcolor(YELLOW); circle(maxx/2,maxy/2,120-i); } pieslice(maxx/2,maxy/2,0,360,5); /* displaying a circle in the middle of clock */ x=maxx/2+100;y=maxy/2; r=100; setcolor(BLUE); for(j=PI/6;j<=(2*PI);j+=(PI/6)) { /* marking the hours for every 30 degrees */ pieslice(x,y,0,360,4); x=(maxx/2)+r*cos(j); y=(maxy/2)+r*sin(j); } x=maxx/2+100;y=maxy/2; r=100; setcolor(RED); for(j=PI/30;j<=(2*PI);j+=(PI/30)) { /* marking the minutes for every 6 degrees */ pieslice(x,y,0,360,2); x=(maxx/2)+r*cos(j); y=(maxy/2)+r*sin(j); } } void secHand() { struct time t; int r=80,x=maxx/2,y=maxy/2,sec; float O; maxx=getmaxx();maxy=getmaxy(); gettime(&t); /*getting the seconds in system clock */ sec=t.ti_sec; O=sec*(PI/30)-(PI/2); /* determining the angle of the line with respect to vertical */ setcolor(YELLOW); line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O)); } void hrHand() { int r=50,hr,min; int x,y; struct time t; float O; maxx=getmaxx(); maxy=getmaxy(); x=maxx/2,y=maxy/2; gettime(&t); /*getting the seconds in system clock */ hr=t.ti_hour; min=t.ti_min; /* determining the angle of the line with respect to vertical */ if(hr<=12)O=(hr*(PI/6)-(PI/2))+((min/12)*(PI/30)); if(hr>12) O=((hr-12)*(PI/6)-(PI/2))+((min/12)*(PI/30)); setcolor(BLUE); line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O)); } void minHand() { int r=60,min; int x,y; float O; struct time t; maxx=getmaxx(); maxy=getmaxy(); x=maxx/2; y=maxy/2; gettime(&t); /*getting the seconds in system clock */ min=t.ti_min; O=(min*(PI/30)-(PI/2)); /* determining the angle of the line with respect to vertical */ setcolor(RED); line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O)); } |

Coment down below if you have any queries related to above program.

Hie, the ctrl+break isn't helping. How do i come out of the output screen.

You can Close the Window directly

Use “while(!kbhit())” instead of “while(1)” in main function.

how would you go about changing the line drawn for the watch hands with pictures, such a .png?

The output displays for a short period of time. Half a second I guess. Huhuhuh

Hey I want to decrease the size and change the position of the clock so that i can show it as wall clock by giving a house background.

the above written code of analog clock get comiled in my computer but whenever I try to run it it shows one error , it says– undefined symbol hrHand () in module

Hi,

I need to get analog clock developed that has date, month, year hands along with hr, min, sec . Any way you could develop it? I can send more detail specs. I am prepared to pay for it.

Thanks.

Regards.

[email protected]

will it run on Code::Block?

no

if u are still active on your blog , i’ve got his problem , i wann know how can i find the coordinates of the line whose length is of radius and also start from the center point of the circle i dont remember much of the trigonometry i learned in 10th basically i just wann create a for loop which prints the image like ashoka chakra from our nations flag i saw the formula you used but i dont have the angle and i didnt understood the way you get the angle so it’lll be really helpful if you elaborate it a little bit

I Liked this code,i hope the author can do it in embedded C using a projector so we can see the time in our hands

this program is incorrcct

thetamin, thetasec is not available

can u give ma other code for my project