1.
Introduction
2.
Digital Clock using C
2.1.
Steps
2.2.
Code
2.3.
Output
3.
3.1.
How to draw a digital clock with the current time?
3.2.
What is the use of the sleep() function?
3.3.
What is the use of the "time.h" header file?
3.4.
How to clear the console in C while displaying the time in the digital clock?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Digital Clock using C

Hari Sapna Nair
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

On average, people who wear digital watches check their watches 4 to 5 times per hour, which means they wake up their digital watches 60 to 80 times per day.

Source: https://gifer.com/en/ARMM

Since we check our digital watches frequently throughout the day, why not learn how to build them?

In this blog, we will learn how to draw a digital clock using C along with the steps and code.

Also Read, Binary to Hex Converter,C Static Function

## Digital Clock using C

In this section, we will learn how to draw a digital clock using C. Before we start learning how to draw a digital clock using C, be sure that you know the C basics like data typeslooping statements, etc.

In this C program, the digital clock will start with the time 00:00:00. Then it will work like a digital clock where it will show the time with hours, minutes, and second.

### Steps

The following steps have to be followed to draw a digital clock using C are:

• Import the required header files.
• Initialize the variables hour, minute, and seconds with 0.
• Run an infinite while loop.
• Increase second and check if it equals 60, then increase minute and reset second to 0.
• Increase minute and check if it equals 60, then increase hour and reset minute to 0.
• Increase hour and check if it equals 24, then reset hour to 0.

Digital Clock using C

Our digital clock will look like the above image. Now let's look at the code to draw a digital clock using C and implement it on Online editor.

Must Read what is storage class in c

### Code

``````// A digital clock using C

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

int main() {
int hour = 0;
int minute = 0;
int second = 0;
while(1) {
// Clear the output on screen
// for windows use system("cls")
system("clear");

// Print the time in HH : MM : SS format
printf("%02d : %02d : %02d ",hour,minute,second);

// Clear the output buffer in gcc
fflush(stdout);

// Increment second
second++;

// Update hour, minute and second
if(second == 60) {
minute += 1;
second = 0;
}

if(minute == 60) {
hour += 1;
minute = 0;
}

if(hour == 24) {
hour = 0;
minute = 0;
second = 0;
}

// Wait for 1 second
sleep(1);
}
return 0;
}``````

### Output

You can practice by yourself with the help of online c compiler.

You can also read about dynamic array in c, and  Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

#### How to draw a digital clock with the current time?

To draw a digital clock with the current time, the C library function localtime can be used. It uses the time pointed by the timer to fill a structure with the values that represent the corresponding local time.

#### What is the use of the sleep() function?

The sleep() function in the C allows us to wait for a current thread for a set amount of time.

#### What is the use of the "time.h" header file?

The "time.h" header file in C contains definitions of functions to get and manipulate date and time information.

#### How to clear the console in C while displaying the time in the digital clock?

We can clear the console in C using the following ways:-

a) By using clrscr(), which is included in the "conio.h" header file.

b) By using system("clear")

c) By using a regex "\e[1;1H\e[2J"

## Conclusion

In this article, we have extensively discussed how to draw a digital clock using C, along with the steps and code.