Kibekityo Juma Shafara
Kibekityo Juma Shafara's Blog

Follow

Kibekityo Juma Shafara's Blog

Follow
The most straightforward C program to print a running digital clock starting from the current time

Photo by Yasin Hasan on Unsplash

The most straightforward C program to print a running digital clock starting from the current time

Kibekityo Juma Shafara's photo
Kibekityo Juma Shafara
·Jul 15, 2022·

2 min read

The C language is a classic, almost everyone in our time started to code using this powerful.

I had written this article like 3 years ago but never really got to publish it. It's elementary stuff but surely someone will find it useful, it's one of those little projects every C coder must try to build, especially in the early days as it breaks down good concepts like loops, pointers, and functions depending on the approach.

In this post, we will learn how to print a digital clock automatically set to the current time

Functions we need to use:

Below are the functions of time.h that we need to use to print the current time values

Function 1:

time_t time(time_t *t).

This returns the time in time_t format. Actually, it returns the Epoch time in seconds since UTC, January 1, 1970. We can pass the parameters as NULL to this function to get the Epoch time. This is the first function that we need to call.

Then we need to call the localtime() function which is defined below

Function 2:

struct tm *localtime(const time_t *t)

This function takes one time_t value as an argument and returns one structure of type tm that holds different time-related values. For printing the hour, minute, and second values, we can use tm_hour, tm_min and tm_sec properties of tm structure.

Next up, we need to set up an infinite while loop to increase seconds, minutes, and hours accordingly

Complete C program

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

int main()
{

    int hrs, min, sec;
    time_t seconds;
    struct tm *timeStruct;

    seconds = time(NULL);
    timeStruct = localtime(&seconds);

    hrs = timeStruct->tm_hour;
    min = timeStruct->tm_min;
    sec = timeStruct->tm_sec;

    while (1)
    {
        system("cls");
        printf("Current time: %.2i : %.2i : %.2i", hrs, min, sec);
        sleep(1);
        sec++;
        if (sec == 60)
        {
            sec = 0;
            min++;
        }
        if (min == 60)
        {
            min = 0;
            hrs++;
        }
        if (hrs == 24)
        {
            hrs = 0;
            min = 0;
            sec = 0;
        }
    }
}

To note,

  • seconds is a variable of type time_t.
  • timeStruct is a tm structure.
  • Running the program will print one output

Current time: 11 : 31: 39

Based on the system time, we will get the hrs, min, and sec values.

When I just coded in C, I hoped to be able to use it for the rest of my life, but since then I have changed direction(web development plus statistics) and C can no longer be the best language for what I do.

Subscribe to my newsletter so you don't miss out on my upcoming articles.

 
Share this