Digital Clock (Stop Watch) Mini Project using C program with Source Code
Contents
Introduction
The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems. Low-level memory access, a small collection of keywords, and a clean style are all qualities that make C language excellent for system programming, such as operating system or compiler development.
Why Learn C Programming Language?
Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.
Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.
Digital Clock (Stop Watch)
Digital clocks have better readability in tight, time-sensitive situations, and some digital models have a countdown timer to help users keep track of time. They may be able to assist in getting pupils to their next class on time.
Wearers of digital watches check their watches 60 to 80 times every day. On average, watch wearers check their smartwatch four to five times per hour, which means they wake it up 60 to 80 times per day. This data comes from an Apple Watch research conducted in 2015.
Since we check at our digital watches frequently through the day, Why not learn how to build it?
The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build digital clock (stop watch) mini project using c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
// for sleep() function
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int hour, minute, second;
hour=minute=second=0;
while(1)
{
// clear output screen
// Incase you get an error here use system("clear") instead of system("cls")
system("cls");
// print time in HH : MM : SS format
printf("%02d : %02d : %02d ",hour,minute,second);
// clear output buffer in gcc
fflush(stdout);
// increase 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;
}
sleep(1); // wait till 1 second
}
return 0;
}
Explanation
- Start with a value of 0 for the hour, minute, and second. i.e. [00 : 00 : 00]
- Make an endless loop.
- Increase the second and check if it equals 60, then increase the minute and return the second to zero.
- Increase the minute until it equals 60, then increase the hour and reset the minute to 0.
- If the hour is equal to 24, increase it and then reset it to 0.
Output
- This is the starting output of the above program.
- Digital Clock (Stop Watch) output screenshot 1.
- Digital Clock (Stop Watch) output screenshot 2.