c語言中的time()函數有什麼作用

#include <time.h>
time_t time( time_t *time );

功能: 函數返回當前時間,如果發生錯誤返回零。如果給定參數time ,那麼當前時間存儲到參數time中。

當前時間是從1970年 1月1日零時開始的秒數.

⑵ c語言time_t ,tm都是些什麼類型

下面是粘貼的哈,將就著看,time_t ---long tm是結構體。
typedef __kernel_time_t time_t;
typedef long __kernel_time_t;

struct tm {
164 /*
165 * the number of seconds after the minute, normally in the range
166 * 0 to 59, but can be up to 60 to allow for leap seconds
167 */
168 int tm_sec;
169 /* the number of minutes after the hour, in the range 0 to 59*/
170 int tm_min;
171 /* the number of hours past midnight, in the range 0 to 23 */
172 int tm_hour;
173 /* the day of the month, in the range 1 to 31 */
174 int tm_mday;
175 /* the number of months since January, in the range 0 to 11 */
176 int tm_mon;
177 /* the number of years since 1900 */
178 long tm_year;
179 /* the number of days since Sunday, in the range 0 to 6 */
180 int tm_wday;
181 /* the number of days since January 1, in the range 0 to 365 */
182 int tm_yday;
183 }

⑶ c語言time函數怎麼用

#include <stdio.h>
#include <time.h>

int main()
{
time_t t1,t2; //分別聲明兩種使用方式的賦值對象

t1=time(0); //第一種使用方式
time(&t2); //第二種使用方式

printf("t1=%ld\n",t1);
printf("t2=%ld",t2);

return 0;
}

⑷ c語言的time函數用法

當NULL來用的吧,可以傳遞一個time_t的指針來獲取當前時間,如果不傳就填NULL,0也可以,這樣取返回值也行

⑸ 關於C語言中內置宏__DATE__和 __TIME__

這兩個就是字元串常量,當字元串用就行。

__DATE__:當前的編譯日期
__TIME__:當前編譯時間;

#include<stdio.h>
#include<string.h>
intmain()
{
printf("%s,%s ",__DATE__,__TIME__);
printf("長度DATE=%d,TIME=%d ",strlen(__DATE__),strlen(__TIME__));
return0;
}

⑹ C語言 time()

1 是把一個正整數放進t所在的地址里,這個正整數是1970年1月1日00:00:00(UTC)開始,到目前為止經過的秒數。

2 因為time以兩種方式返回結果。一種是你第一道題的,給他一個地址,他把結果寫進那個地址。第二種直接返回一個time_t。你可以用這種方法接:time_t t = time(NULL)。這里給他一個空指針就是告訴他不需要以第一種方法返回結果,所以當然他也不會把結果寫進空指針,他只是不管這個參數而已。當然只要你樂意你也可以兩種一起用。

⑺ c語言中time函數的用法

頭文件time.h

@函數名稱: localtime
函數原型: struct tm *localtime(const time_t *timer)
函數功能: 返回一個以tm結構表達的機器時間信息
函數返回: 以tm結構表達的時間,結構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;
};
參數說明: timer-使用time()函數獲得的機器時間

#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;
}

@函數名稱: asctime
函數原型: char* asctime(struct tm * ptr)
函數功能: 得到機器時間(日期時間轉換為ASCII碼)
函數返回: 返回的時間字元串格式為:星期,月,日,小時:分:秒,年
參數說明: 結構指針ptr應通過函數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;
}

@函數名稱: ctime
函數原型: char *ctime(long time)
函數功能: 得到日歷時間
函數返回: 返回字元串格式:星期,月,日,小時:分:秒,年
參數說明: time-該參數應由函數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;
}

@函數名稱: difftime
函數原型: double difftime(time_t time2, time_t time1)
函數功能: 得到兩次機器時間差,單位為秒
函數返回: 時間差,單位為秒
參數說明: time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得
所屬文件: <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;
}

@函數名稱: gmtime
函數原型: struct tm *gmtime(time_t *time)
函數功能: 得到以結構tm表示的時間信息
函數返回: 以結構tm表示的時間信息指針
參數說明: time-用函數time()得到的時間信息
所屬文件: <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;
}

@函數名稱: time
函數原型: time_t time(time_t *timer)
函數功能: 得到機器的日歷時間或者設置日歷時間
函數返回: 機器日歷時間
參數說明: timer=NULL時得到機器日歷時間,timer=時間數值時,用於設置日歷時間,time_t是一個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;
}

@函數名稱: tzset
函數原型: void tzset(void)
函數功能: UNIX兼容函數,用於得到時區,在DOS環境下無用途
函數返回:
參數說明:
所屬文件: <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;
}

⑻ c語言中time參數

time_t
time(
time_t
*time
);
函數time()返回當前時間,若參數time給定,則當前時間存儲在time指針中。
注意,這個參數的類型是time_t
*,即一個指針,如果你傳的是0的話,會被自動看成NULL,空指針,肯定是無法保存當前時間的。
所以time()函數的實際功能是:如果參數為0則函數返回值即為結果,若參數不為0,則結果保存在參數中。

⑼ C語言時間函數time_t

電腦cpu的主頻太高,一百萬次空循環的執行時間太短,所以,difftime(返回時間差)的返回值非常非常小,比如他返回的是0.0002的話,實際上不是0,但是因你輸出時,只保留兩位小數,後面的不是0的值也沒輸出,所以你保留兩位小數輸出的話,肯定是0.00
另外,difftime的返回值是64位整數,太小的話,肯定也是零,說到底還是因為時間差太小的造成的

循環次數加大一些,現在cpu主頻都是1-2g的,你設1億次或者10億應該才能看到差距
for (i = 100000000; i > 0; --i);
改成1億次。