May 16, 2025
#include <stdlib.h>#include <time.h>#include <stdbool.h>#include <unistd.h> (for Linux/Mac) or #include <windows.h> (for Windows)time_t raw_time;struct tm *p_time;bool is_running = true;*srand(time(NULL));while loop to continue while is_running is true.sleep(1); to update every second.raw_time using time(&raw_time);p_time = localtime(&raw_time); to convert to local time.printf() with formatting:
Hour: %02d, Minute: %02d, Second: %02dp_time->tm_hour, p_time->tm_min, p_time->tm_secis_running = false; to exit the loop if needed.#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int main() {
time_t raw_time;
struct tm *p_time;
bool is_running = true;
printf("Digital Clock\n");
while (is_running) {
time(&raw_time);
p_time = localtime(&raw_time);
printf("%02d:%02d:%02d\r", p_time->tm_hour, p_time->tm_min, p_time->tm_sec);
fflush(stdout);
sleep(1);
}
return 0;
}