當前位置:首頁 » 編程語言 » c語言time
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言time

發布時間: 2022-01-20 16:12:49

1. c語言time問題

時間太短。
你把 t = clock(); 移到外面:

int main ( )
{
int n,m;
clock_t t ;

t = clock(); // 移到這里
for (n=6;n<=20000;n+=2){
for (m=3;m<=n/2;m+=2){
// t = clock(); 這個t去掉
if (isprime(m)&&isprime(n-m)){
printf("%d,%fs\n",n,(double)(clock()-t)/CLK_TCK);
break;}
}

2. C語言 time()

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

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

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

time是C語言獲取當前系統時間的函數,以秒作單位,代表當前時間自Unix標准時間戳(1970年1月1日0點0分0秒,GMT)經過了多少秒。

形式為time_t time(time_t * t);

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

可以根據需要選擇。當參數t為空指針(NULL)時,只返回值。

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

(3)c語言time擴展閱讀:

time函數

函數名稱: localtime

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

函數功能: 返回一個以tm結構表達的機器時間信息

函數返回: 以tm結構表達的時間,結構tm定義如下:

#ifndef _TM_DEFINED

struct tm {

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

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

int tm_hour; /* 時 - 取值區間為[0,23] */

int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */

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

int tm_year; /* 年份,其值等於實際年份減去1900 */

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

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

int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/

};

#define _TM_DEFINED

#endif

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

4. c語言的time函數用法

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

5. 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 }

6. 關於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;
}

7. C語言 time(NULL)

c語言中 srand(time(NULL)); 的意思是:使用當前時間進行隨機數發生器的初始化。

time_t time(time_t *t); 是C標准庫函數,如果t是空指針(NULL),直接返回當前時間。如果t不是空指針,返回當前時間的同時,將返回值賦予t指向的內存空間。

time() 是指返回自 Unix 紀元起的當前時間的秒數的函數,主要用來獲取當前的系統時間,返回的結果是一個time_t類型。

srand函數是隨機數發生器的初始化函數。原型:void srand(unsigned int seed); srand和rand()配合使用產生偽隨機數序列。

time()的定義和用法

其值表示從UTC(Coordinated Universal Time)時間1970年1月1日00:00:00(稱為UNIX系統的Epoch時間)到當前時刻的秒數。然後調用localtime函數將time_t所表示的UTC時間轉換為本地時間(我們是+8區,比UTC多8個小時)並轉成struct tm類型,該類型的各數據成員分別表示年月日時分秒。

功能說明:srand設置產生一系列偽隨機數發生器的起始點,要想把發生器重新初始化,可用1作seed值。任何共它的值都把發生器匿成一個隨機的起始點。rand檢索生成的偽隨機數。在任何調用srand之前調用rand與以1作為seed調用srand產生相同的序列。

此函數可以設定rand函數所用的隨機數產生演演算法的種子值。任何大於一的種子值都會將rand隨機數所產生的虛擬隨機數序列重新設定一個起始點。

8. C語言時間函數time_t

1、time_t // 時間類型(time.h 定義)
struct tm { // 時間結構,time.h 定義如下:
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;
}
time ( &rawtime ); // 獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime ( &rawtime ); //轉為當地時間,tm 時間結構
asctime() // 轉為標准ASCII時間格式:
//就是直接列印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1

2、time函數使用示例

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

9. 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;
}