This blog is under construction

Monday 8 July 2013

C program to implement digital clock

Write a C program to implement digital clock.


  #include <stdio.h>
  #include <time.h>
  int main() {
        time_t ts, flag = 1;
        struct tm *ct;
        system("clear");
        printf("\n\n\n\tDIGITAL CLOCK\n\t  ");

        /* digital clock implementation */
        while (1) {

                /* time() - gives time in seconds */
                ts = time(NULL);

                /* converting time(NULL) o/p to min, sec, hours */
                ct = localtime(&ts);

                /* print the digital clock */
                if (flag || ct->tm_min % 59 == 0) {
                        if (!flag)
                                printf("\b\b\b\b\b\b");
                        printf("%02d:%02d:%02d",
                                ct->tm_hour, ct->tm_min, ct->tm_sec);

                        flag = 0;
                        printf("\b\b");
                        continue;
                }

                if (ct->tm_sec % 59 == 0) {
                        printf("\b\b\b");
                        printf("%02d:", ct->tm_min);
                }

                printf("%02d", ct->tm_sec);
                printf("\b\b");
        }
        return 0;
  }



  Output:

DIGITAL CLOCK
 18:38:15






See Also:

No comments:

Post a Comment