| ⏰ Real-time Display | 🗓️ Date Display | 🔄 12/24 Format | 🖥️ Terminal UI | ⚡ system time.h |
|---|---|---|---|---|
| Live clock updating every second in the terminal | Shows current date alongside the time | Toggle between 12-hour AM/PM and 24-hour format | Clean formatted output directly in the console | Powered by C standard library time functions |
╔══════════════════════════════════════════════╗
║ ║
║ DIGITAL CLOCK — C PROGRAM ║
║ ║
║ ┌──────────────────────────────────────┐ ║
║ │ │ ║
║ │ 🕐 03 : 42 : 17 PM │ ║
║ │ │ ║
║ │ 📅 Thursday, 19 Mar 2026 │ ║
║ │ │ ║
║ └──────────────────────────────────────┘ ║
║ ║
║ Format : [ 12-hr ] [ 24-hr ] ║
║ Press : Ctrl+C to exit ║
║ ║
╚══════════════════════════════════════════════╝
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime); // get current time as seconds since epoch
timeinfo = localtime(&rawtime); // convert to local time struct
printf("Time: %02d:%02d:%02d\n",
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec);
printf("Date: %02d/%02d/%04d\n",
timeinfo->tm_mday,
timeinfo->tm_mon + 1,
timeinfo->tm_year + 1900);
return 0;
}int hour = timeinfo->tm_hour;
char *period = "AM";
if (hour >= 12) {
period = "PM";
if (hour > 12) hour -= 12; // convert to 12-hr
} else if (hour == 0) {
hour = 12; // midnight edge case
}#include <windows.h> // for Windows Sleep()
while (1) {
system("cls"); // clear terminal screen
// ... print clock ...
Sleep(1000); // wait 1 second then refresh
}gcc -o digital_clock "(PROJECT)_Digital_Clock.c"./digital_clock# Open the folder in VS Code, then in the terminal:
gcc -o digital_clock "(PROJECT)_Digital_Clock.c" && ./digital_clock
⚠️ This project useswindows.handsystem("cls")— designed for Windows only. On Linux/macOS replaceSleep(1000)withsleep(1)andsystem("cls")withsystem("clear").
Project-C-Digital-Clock/
│
├── 📄 (PROJECT)_Digital_Clock.c ← Full source code · single file
└── 📄 README.md ← Project documentation
┌──────────────────────────────────────────────────┐
│ C Console Application │
├─────────────────┬────────────────────────────────┤
│ Language │ C (C99 standard) │
│ Compiler │ MinGW GCC (Windows) │
│ Editor │ VS Code │
│ Libraries │ stdio.h · time.h · windows.h │
│ Platform │ Windows │
└─────────────────┴────────────────────────────────┘
time() → returns current time as seconds since Jan 1, 1970
localtime() → converts time_t to local tm struct
tm_hour → hour field (0-23) from tm struct
tm_min → minute field (0-59) from tm struct
tm_sec → second field (0-59) from tm struct
tm_mday → day of month from tm struct
tm_mon → month (0-11, so +1 for display)
tm_year → years since 1900 (so +1900 for display)
Sleep(1000) → pauses execution for 1000ms (Windows)
system("cls") → clears the terminal screen (Windows)
Saharia Hassan Safin C Developer · Front-end Developer
"Time flies — so I built a clock to watch it" 🕐