Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

C program พิมพ์นาฬิกาดิจิตอลพร้อมเวลาปัจจุบัน


ในส่วนนี้เราจะดูวิธีทำ digital clock ด้วย C ทำงานกับเวลาใช้ time.h header file มี function signatures สำหรับจัดการ date และ time

สี่ components สำคัญของ time.h:

  • size_t unsigned integral type ผลลัพธ์ของ sizeof()

  • clock_t ใช้เก็บ processor time

  • time_t ใช้เก็บ calendar time

  • struct tm structure ช่วยเก็บ date และ time ทั้งหมด

ตัวอย่างโค้ด

#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