Friday 7 March 2014

How to make Digital Clock in C language

I have made a digital watch in C language .It is similar to the program of stop watch which I have posted earlier. This program simply takes the system time and prints it on the output screen and after a delay of every 100 milliseconds it updates the time.




 SOURCE CODE OUTPUT EXPLANATION DOWNLOAD


#include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include <time.h>
#include <dos.h>
int main(void)
{
   struct  time t;
   while(!kbhit())
   {
   gettime(&t);
   clrscr();
   printf("\n\t\t\tDIGITAL WATCH") ;
   printf("\n\n\n\t\t\t %2d:%02d:%02d.%02d\n",
 t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
   printf("\n\n\npress any key to exit");
   delay(100);
   }
   return 0;
}

SCREENSHOTS

Previous                                                                 Next
We have already discuss about kbhit()and delay() functions .Now let us understand the program from starting .First we have declare structure type variable of time t .The struct time is already defined in dos.h header file and is as follows
struct  time    {
    unsigned char   ti_min;     /* Minutes */
    unsigned char   ti_hour;    /* Hours */
    unsigned char   ti_hund;    /* Hundredths of seconds */
    unsigned char   ti_sec;     /* Seconds */
};
Now we will get system time through gettime() function ,it is also defined in dos.h header  file .In this function we will pass the address of struct variable t and gettime() fills time structure with system time. Now we will print the system time on the screen and after a delay of every 100 milliseconds we will update it.
 Any questions regarding to program please write in comments.

No comments:

Post a Comment