使用 C 語言編寫程式列印帶有當前時間的數字時鐘
在本節中,我們將看到如何使用 C 語言製作數字時鐘。要處理時間,我們可以使用 time.h 標頭檔案。此標頭檔案有一些函式簽名,用於處理日期和時間相關問題。
time.h 的四個重要組成部分如下
size_t 此 size_t 是無符號整數型別。它是 sizeof() 的結果。
clock_t 用於儲存處理器時間
time_t 用於儲存日曆時間
struct tm 這是一個結構。它用於儲存完整的日期和時間。
示例程式碼
#include <stdio.h> #include <time.h> int main() { time_t s, val = 1; struct tm* curr_time; s = time(NULL); //This will store the time in seconds curr_time = localtime(&s); //get the current time using localtime() function //Display in HH:mm:ss format printf("%02d:%02d:%02d", curr_time->tm_hour, curr_time->tm_min, curr_time->tm_sec); }
輸出
23:35:44
廣告