c語言time函數(shù)作用 c語言的time函數(shù)

C語言 time(NULL)

C語言time(NULL)是以當(dāng)前時(shí)間為種子,產(chǎn)生隨意數(shù)。

創(chuàng)新互聯(lián)-企業(yè)級(jí)云服務(wù)器提供商,為用戶提供云服務(wù)器、CDN、云安全服務(wù)、服務(wù)器托管、服務(wù)器租用、高防主機(jī)等全方位云服務(wù)與各行業(yè)解決方案,幫助企業(yè)及個(gè)人極速備案,輕松上云,安全無憂。

其中,time(NULL)用來獲取當(dāng)前時(shí)間,本質(zhì)上得到的是一個(gè)大整數(shù),然后用這個(gè)數(shù)來隨機(jī)數(shù)。

time()這個(gè)函數(shù)其實(shí)保存的是一個(gè)歷史時(shí)間,所以需要用NULL把這個(gè)歷史時(shí)間清空一下,time()就會(huì)自動(dòng)保存當(dāng)前時(shí)間了。你可以簡單的理解為NULL就是給time()初始化。

c語言調(diào)用time()函數(shù)括號(hào)里為什么要用NULL?

time是這樣聲明的:time_ttime(time_t*timer)

用法是你先自己定義一個(gè)time_t變量,讓后把變量的地址傳給它。函數(shù)會(huì)返回自1970年1月1日0點(diǎn)走過的秒數(shù),同時(shí)把這個(gè)返回值保存在你傳進(jìn)來的那個(gè)time_t*指向的變量里面

如果你傳進(jìn)來NULL的話,就不保存。

C語言中的time()函數(shù)有什么作用

#include time.h

time_t time( time_t *time );

功能: 函數(shù)返回當(dāng)前時(shí)間,如果發(fā)生錯(cuò)誤返回零。如果給定參數(shù)time ,那么當(dāng)前時(shí)間存儲(chǔ)到參數(shù)time中。

當(dāng)前時(shí)間是從1970年 1月1日零時(shí)開始的秒數(shù).

C語言中time(0)的意思是?

time是C語言獲取當(dāng)前系統(tǒng)時(shí)間的函數(shù),以秒作單位,代表當(dāng)前時(shí)間自Unix標(biāo)準(zhǔn)時(shí)間戳(1970年1月1日0點(diǎn)0分0秒,GMT)經(jīng)過了多少秒。

形式為time_t time(time_t * t);

該函數(shù)提供兩種返回方式,返回值,和指針參數(shù)。

可以根據(jù)需要選擇。當(dāng)參數(shù)t為空指針(NULL)時(shí),只返回值。

而NULL的定義是(void *) 0, 所以time(0)也就是time(NULL)的另一種寫法,表示只通過返回值獲取時(shí)間值。

擴(kuò)展資料:

time函數(shù)

函數(shù)名稱: localtime

函數(shù)原型: struct tm *localtime(const time_t *timer)

函數(shù)功能: 返回一個(gè)以tm結(jié)構(gòu)表達(dá)的機(jī)器時(shí)間信息

函數(shù)返回: 以tm結(jié)構(gòu)表達(dá)的時(shí)間,結(jié)構(gòu)tm定義如下:

#ifndef _TM_DEFINED

struct tm {

int tm_sec; /* 秒 – 取值區(qū)間為[0,59] */

int tm_min; /* 分 - 取值區(qū)間為[0,59] */

int tm_hour; /* 時(shí) - 取值區(qū)間為[0,23] */

int tm_mday; /* 一個(gè)月中的日期 - 取值區(qū)間為[1,31] */

int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */

int tm_year; /* 年份,其值等于實(shí)際年份減去1900 */

int tm_wday; /* 星期 – 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類推 */

int tm_yday; /* 從每年的1月1日開始的天數(shù) – 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */

int tm_isdst; /* 夏令時(shí)標(biāo)識(shí)符,實(shí)行夏令時(shí)的時(shí)候,tm_isdst為正。不實(shí)行夏令時(shí)的進(jìn)候,tm_isdst為0;不了解情況時(shí),tm_isdst()為負(fù)。*/

};

#define _TM_DEFINED

#endif

參數(shù)說明: timer-使用time()函數(shù)獲得的機(jī)器時(shí)間

參考資料來源:百度百科-time.h

c語言中time函數(shù)怎么用?

頭文件time.h

@函數(shù)名稱: localtime

函數(shù)原型: struct tm *localtime(const time_t *timer)

函數(shù)功能: 返回一個(gè)以tm結(jié)構(gòu)表達(dá)的機(jī)器時(shí)間信息

函數(shù)返回: 以tm結(jié)構(gòu)表達(dá)的時(shí)間,結(jié)構(gòu)tm定義如下:

struct tm{

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

};

參數(shù)說明: timer-使用time()函數(shù)獲得的機(jī)器時(shí)間

#include time.h

#include stdio.h

#include dos.h

int main()

{

time_t timer;

struct tm *tblock;

timer=time(NULL);

tblock=localtime(timer);

printf("Local time is: %s",asctime(tblock));

return 0;

}

@函數(shù)名稱: asctime

函數(shù)原型: char* asctime(struct tm * ptr)

函數(shù)功能: 得到機(jī)器時(shí)間(日期時(shí)間轉(zhuǎn)換為ASCII碼)

函數(shù)返回: 返回的時(shí)間字符串格式為:星期,月,日,小時(shí):分:秒,年

參數(shù)說明: 結(jié)構(gòu)指針ptr應(yīng)通過函數(shù)localtime()和gmtime()得到

所屬文件: time.h

#include stdio.h

#include string.h

#include time.h

int main()

{

struct tm t;

char str[80];

t.tm_sec=1;

t.tm_min=3;

t.tm_hour=7;

t.tm_mday=22;

t.tm_mon=11;

t.tm_year=56;

t.tm_wday=4;

t.tm_yday=0;

t.tm_isdst=0;

strcpy(str,asctime(t));

printf("%s",str);

return 0;

}

@函數(shù)名稱: ctime

函數(shù)原型: char *ctime(long time)

函數(shù)功能: 得到日歷時(shí)間

函數(shù)返回: 返回字符串格式:星期,月,日,小時(shí):分:秒,年

參數(shù)說明: time-該參數(shù)應(yīng)由函數(shù)time獲得

所屬文件: time.h

#include stdio.h

#include time.h

int main()

{

time_t t;

time(t);

printf("Today's date and time: %s",ctime(t));

return 0;

}

@函數(shù)名稱: difftime

函數(shù)原型: double difftime(time_t time2, time_t time1)

函數(shù)功能: 得到兩次機(jī)器時(shí)間差,單位為秒

函數(shù)返回: 時(shí)間差,單位為秒

參數(shù)說明: time1-機(jī)器時(shí)間一,time2-機(jī)器時(shí)間二.該參數(shù)應(yīng)使用time函數(shù)獲得

所屬文件: time.h

#include time.h

#include stdio.h

#include dos.h

#include conio.h

int main()

{

time_t first, second;

clrscr();

first=time(NULL);

delay(2000);

second=time(NULL);

printf("The difference is: %f seconds",difftime(second,first));

getch();

return 0;

}

@函數(shù)名稱: gmtime

函數(shù)原型: struct tm *gmtime(time_t *time)

函數(shù)功能: 得到以結(jié)構(gòu)tm表示的時(shí)間信息

函數(shù)返回: 以結(jié)構(gòu)tm表示的時(shí)間信息指針

參數(shù)說明: time-用函數(shù)time()得到的時(shí)間信息

所屬文件: time.h

#include stdio.h

#include stdlib.h

#include time.h

#include dos.h

char *tzstr="TZ=PST8PDT";

int main()

{

time_t t;

struct tm *gmt, *area;

putenv(tzstr);

tzset();

t=time(NULL);

area=localtime(t);

printf("Local time is:%s", asctime(area));

gmt=gmtime(t);

printf("GMT is:%s", asctime(gmt));

return 0;

}

@函數(shù)名稱: time

函數(shù)原型: time_t time(time_t *timer)

函數(shù)功能: 得到機(jī)器的日歷時(shí)間或者設(shè)置日歷時(shí)間

函數(shù)返回: 機(jī)器日歷時(shí)間

參數(shù)說明: timer=NULL時(shí)得到機(jī)器日歷時(shí)間,timer=時(shí)間數(shù)值時(shí),用于設(shè)置日歷時(shí)間,time_t是一個(gè)long類型

所屬文件: time.h

#include time.h

#include stdio.h

#include dos.h

int main()

{

time_t t;

t=time();

printf("The number of seconds since January 1,1970 is %ld",t);

return 0;

}

@函數(shù)名稱: tzset

函數(shù)原型: void tzset(void)

函數(shù)功能: UNIX兼容函數(shù),用于得到時(shí)區(qū),在DOS環(huán)境下無用途

函數(shù)返回:

參數(shù)說明:

所屬文件: time.h

#include time.h

#include stdlib.h

#include stdio.h

int main()

{

time_t td;

putenv("TZ=PST8PDT");

tzset();

time(td);

printf("Current time=%s",asctime(localtime(td)));

return 0;

}

分享標(biāo)題:c語言time函數(shù)作用 c語言的time函數(shù)
新聞來源:http://bm7419.com/article38/dohpipp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站設(shè)計(jì)公司外貿(mào)建站、品牌網(wǎng)站建設(shè)云服務(wù)器、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司