三、時間函式
c語言中使用時間和日期函式時,需要新增標頭檔案,clock_t和time_t是兩個表示時間的算數型別,struct tm用於儲存日曆時間的各個組成部分。
clock_t定義:
time_t定義:#ifndef __clock_t_defined
#define __clock_t_defined
// 避免重複定義 clock_t
typedef
long clock_t;
//clock_t是乙個長整形數
#endif
#ifndef __time_t
#define __time_t
//避免重複定義 time_t
typedef
long time_t;
//時間值time_t 是乙個長整型
#endif
可以使用的函式是gmtime()和localtime()將time()獲得的日曆時間time_t結構體轉換成tm結構體。struct tm
;
**:
結果:#include
#include
intmain
(void
)
列舉幾個簡單的時間函式:second:
20minute:
13hour:
14mday:
22month:
8year:
2020
clock函式返回程式開始執行後占用的處理器時間。如果無法獲取處理器時間,則返回值為-1。
time函式返回當前日曆時間。如果無法獲取日曆時間,則返回值為-1。
difftime函式返回time2-time1的值(以秒為單位。
mktime函式將*tp中的當地時間轉換為與time表示方式相同的日曆時間。
C語言筆記 time h標準庫操作時間相關問題
在c語言的標頭檔案time.h中,定義了日期和時間的資料型別以及函式。c語言中有兩種方式表示時間日期的值 time t型別和struct tm型別。以下是time.h標頭檔案的常用的時間資料型別截圖 typedef long clock t typedef int32 time32 t typede...
c語言time h函式庫小結
一 重要的巨集 clocks per sec 每秒鐘的滴答數 clock t 滴答計數型別 time t 日曆時間的秒計數 二 重要資料型別 struct tm 年月日等得細分時間資料結構 三 全部函式 clock t clock void 返回本程式自啟動來,流逝的時鐘滴答數。double dif...
C 標準庫總結
演算法 accumulate 在 include 中,accumulate 作用是對一對表示範圍的迭代器中的元素進行相加。如 accumulate v.begin v.end 0 表示將v中的元素以0為初始值進行相加。第三個引數表示 的型別。注意 accumulate vs.begin vs.end...