c語言strptine,c語言strptime的返回值

本文目錄一覽:

C語言 輸入一個整數 計算並打印它的數字中有多少個7

1.只要將數值逐位取余,進行判斷即可。

#include stdio.h

int main()

{

    int num = 0, cnt = 0;

    printf(“input a num:”);

    scanf(“%d”, num);

    while (num)

    {

       if (num%10 == 7)  // 逐位取余

           cnt++;

       num /= 10;

    }

    printf(“It has %d.\n”, cnt);

    return 0;

}

c語言時間處理函數

C語言的標準庫函數包括一系列日期和時間處理函數,它們都在頭文件中說明。

在頭文件中定義了三種類型:time_t,struct tm和clock_t。

下面列出了這些函數。

time_t time(time_t *timer);

double difftime(time_t time1,time_t time2);

struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timer);

char *asctime(const struct tm *timeptr);

char *ctime(const time_t *timer);

size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr);

time_t mktime(struct tm *timeptr);

clock_t clock(void);

【具體應用舉例】

asctime(將時間和日期以字符串格式表示)

相關函數

time,ctime,gmtime,localtime

表頭文件

#i nclude

定義函數

char * asctime(const struct tm * timeptr);

函數說明

asctime()將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。

此函數已經由時區轉換成當地時間,字符串格式為:”Wed Jun 30 21:49:08 1993\n”

返回值

若再調用相關的時間日期函數,此字符串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。

附加說明

返回一字符串表示目前當地的時間日期。

範例

#i nclude

main()

{

time_t timep;

time (timep);

printf(“%s”,asctime(gmtime(timep)));

}

執行

Sat Oct 28 02:10:06 2000

 

ctime(將時間和日期以字符串格式表示)

相關函數

time,asctime,gmtime,localtime

表頭文件

#i nclude

定義函數

char *ctime(const time_t *timep);

函數說明

ctime ()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。

此函數已經由時區轉換成當地時間,字符串格式為”Wed Jun 30 21 :49 :08 1993\n”。若再調用相關的時間日期函數,此字符串可能會被破壞。

返回值

返回一字符串表示目前當地的時間日期。

範例

#i nclude

main()

{

time_t timep;

time (timep);

printf(“%s”,ctime(timep));

}

執行

Sat Oct 28 10 : 12 : 05 2000

 

gettimeofday(取得目前的時間)

相關函數

time,ctime,ftime,settimeofday

表頭文件

#i nclude

#i nclude

定義函數

int gettimeofday ( struct timeval * tv , struct timezone * tz )

函數說明

gettimeofday()會把目前的時間有tv所指的結構返回,當地時區的信息則放到tz所指的結構中。

timeval結構定義為:

struct timeval{

long tv_sec; /*秒*/

long tv_usec; /*微秒*/

};

timezone 結構定義為:

struct timezone{

int tz_minuteswest; /*和Greenwich 時間差了多少分鐘*/

int tz_dsttime; /*日光節約時間的狀態*/

};

上述兩個結構都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態如下

DST_NONE /*不使用*/

DST_USA /*美國*/

DST_AUST /*澳洲*/

DST_WET /*西歐*/

DST_MET /*中歐*/

DST_EET /*東歐*/

DST_CAN /*加拿大*/

DST_GB /*大不列顛*/

DST_RUM /*羅馬尼亞*/

DST_TUR /*土耳其*/

DST_AUSTALT /*澳洲(1986年以後)*/

返回值

成功則返回0,失敗返回-1,錯誤代碼存於errno。附加說明EFAULT指針tv和tz所指的內存空間超出存取權限。

範例

#i nclude

#i nclude

main(){

struct timeval tv;

struct timezone tz;

gettimeofday (tv , tz);

printf(“tv_sec; %d\n”, tv,.tv_sec) ;

printf(“tv_usec; %d\n”,tv.tv_usec);

printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);

printf(“tz_dsttime, %d\n”,tz.tz_dsttime);

}

執行

tv_sec: 974857339

tv_usec:136996

tz_minuteswest:-540

tz_dsttime:0

 

gmtime(取得目前時間和日期)

相關函數

time,asctime,ctime,localtime

表頭文件

#i nclude

定義函數

struct tm*gmtime(const time_t*timep);

函數說明

gmtime()將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構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;

};

int tm_sec 代表目前秒數,正常範圍為0-59,但允許至61秒

int tm_min 代表目前分數,範圍0-59

int tm_hour 從午夜算起的時數,範圍為0-23

int tm_mday 目前月份的日數,範圍01-31

int tm_mon 代表目前月份,從一月算起,範圍從0-11

int tm_year 從1900 年算起至今的年數

int tm_wday 一星期的日數,從星期一算起,範圍為0-6

int tm_yday 從今年1月1日算起至今的天數,範圍為0-365

int tm_isdst 日光節約時間的旗標

此函數返回的時間日期未經時區轉換,而是UTC時間。

返回值

返回結構tm代表目前UTC 時間

範例

#i nclude

main(){

char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};

time_t timep;

struct tm *p;

time(timep);

p=gmtime(timep);

printf(“%d%d%d”,(1900+p-tm_year), (1+p-tm_mon),p-tm_mday);

printf(“%s%d;%d;%d\n”, wday[p-tm_wday], p-tm_hour, p-tm_min, p-tm_sec);

}

執行

2000/10/28 Sat 8:15:38

 

localtime(取得當地目前時間和日期)

相關函數

time, asctime, ctime, gmtime

表頭文件

#i nclude

定義函數

struct tm *localtime(const time_t * timep);

函數說明

localtime()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。

結構tm的定義請參考gmtime()。此函

數返回的時間日期已經轉換成當地時區。

返回值

返回結構tm代表目前的當地時間。

範例

#i nclude

main(){

char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};

time_t timep;

struct tm *p;

time(timep);

p=localtime(timep); /*取得當地時間*/

printf (“%d%d%d “, (1900+p-tm_year),( l+p-tm_mon), p-tm_mday);

printf(“%s%d:%d:%d\n”, wday[p-tm_wday],p-tm_hour, p-tm_min, p-tm_sec);

}

執行

2000/10/28 Sat 11:12:22

 

mktime(將時間結構數據轉換成經過的秒數)

相關函數

time,asctime,gmtime,localtime

表頭文件

#i nclude

定義函數

time_t mktime(strcut tm * timeptr);

函數說明

mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。

返回值

返回經過的秒數。

範例

/* 用time()取得時間(秒數),利用localtime()

轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數*/

#i nclude

main()

{

time_t timep;

strcut tm *p;

time(timep);

printf(“time() : %d \n”,timep);

p=localtime(timep);

timep = mktime(p);

printf(“time()-localtime()-mktime():%d\n”,timep);

}

執行

time():974943297

time()-localtime()-mktime():974943297

 

settimeofday(設置目前時間)

相關函數

time,ctime,ftime,gettimeofday

表頭文件

#i nclude

#i nclude

定義函數

int settimeofday ( const struct timeval *tv,const struct timezone *tz);

函數說明

settimeofday()會把目前時間設成由tv所指的結構信息,當地時區信息則設成tz所指的結構。詳細的說明請參考gettimeofday()。

注意,只有root權限才能使用此函數修改時間。

返回值

成功則返回0,失敗返回-1,錯誤代碼存於errno。

錯誤代碼

EPERM 並非由root權限調用settimeofday(),權限不夠。

EINVAL 時區或某個數據是不正確的,無法正確設置時間。

 

time(取得目前的時間)

相關函數

ctime,ftime,gettimeofday

表頭文件

#i nclude

定義函數

time_t time(time_t *t);

函數說明

此函數會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指針的話,

此函數也會將返回值存到t指針所指的內存。

返回值

成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存於errno中。

範例

#i nclude

mian()

{

int seconds= time((time_t*)NULL);

printf(“%d\n”,seconds);

}

Date and Time Functions: time.h

The header time.h declares types and functions for manipulating date and time. Some functions process local time,

which may differ from calendar time, for example because of time zone. clock_t and time_t are arithmetic types

representing times, and struct tm holds the components

of a calendar time:

int tm_sec; seconds after the minute (0,61)

int tm_min; minutes after the hour (0,59)

int tm_hour; hours since midnight (0,23)

int tm_mday; day of the month (1,31)

int tm_mon; months since January (0,11)

int tm_year; years since 1900

int tm_wday; days since Sunday (0,6)

int tm_yday; days since January 1 (0,365)

int tm_isdst; Daylight Saving Time flag

tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available.

clock_t clock(void)

clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable.

clock()/CLK_PER_SEC is a time in

seconds.

time_t time(time_t *tp)

time returns the current calendar time or -1 if the time is not available. If tp is not NULL,

the return value is also assigned to *tp.

double difftime(time_t time2, time_t time1)

difftime returns time2-time1 expressed in seconds.

time_t mktime(struct tm *tp)

mktime converts the local time in the structure *tp into calendar time in the same representation used by time.

The components will have values

in the ranges shown. mktime returns the calendar time or -1 if it cannot be represented.

The next four functions return pointers to static objects that may be overwritten by other calls.

char *asctime(const struct tm *tp)

asctime*tp into a string of the form

Sun Jan 3 15:14:13 1988\n\0

char *ctime(const time_t *tp)

ctime converts the calendar time *tp to local time; it is equivalent to

asctime(localtime(tp))

struct tm *gmtime(const time_t *tp)

gmtime converts the calendar time *tp into Coordinated Universal Time (UTC). It returns NULL if UTC is not available.

The name gmtime has historical significance.

struct tm *localtime(const time_t *tp)

localtime converts the calendar time *tp into local time.

size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)

strftime formats date and time information from *tp into s according to fmt, which is analogous to a printf format.

Ordinary characters (including the terminating ‘\0’) are copied into s. Each %c is replaced as described below,

using values appropriate for the local environment.

No more than smax characters are placed into s. strftime returns the number of characters, excluding the ‘\0’,

or zero if more than smax characters were produced.

%a abbreviated weekday name.

%A full weekday name.

%b abbreviated month name.

%B full month name.

%c local date and time representation.

%d day of the month (01-31).

%H hour (24-hour clock) (00-23).

%I hour (12-hour clock) (01-12).

%j day of the year (001-366).

%m month (01-12).

%M minute (00-59).

%p local equivalent of AM or PM.

%S second (00-61).

%U week number of the year (Sunday as 1st day of week) (00-53).

%w weekday (0-6, Sunday is 0).

%W week number of the year (Monday as 1st day of week) (00-53).

%x local date representation.

%X local time representation.

%y year without century (00-99).

%Y year with century.

%Z time zone name, if any.

%% %

C語言程序怎麼設計日期?

數據結構 :

#include time.h stringude ring.h #includestdio.h ,/*在time.h中定義的結構體類型 struct tm

設計時間模塊列表 :

TodayTime_f(Time *); /*返回當前時間、日期*/ Print_f(Time); /*打印時間 */ Input_f(Time *)  /* 輸入時間*/ int IsLeapYear_f(int ); /*判斷是否閏年*/ int FindDaysInMonth_f( int nYear,  int nMon); /*

返回指定月份的天數*/ void AddDay_f(Time *strpTime, int nDay); /* 結構體變量加上天數*/ AddTime_f (Time *strpBas, Time *strpNum);  /*時間相加*/ void TimeAdd_f(); /* 時間相加*/ void Initial(void); /*

初始化信息*/ TodayTime_f(Time *strpTime) { time_t  strCurrTime = time(0);/* 取當前時間*/   struct tm *strpCurr = localtime(strCurrTime);   strpTime-nDay = strpCurr-tm_mday;   strpTime-nYear = (strpCurr-tm_year+1900);   strpTime-nMon = strpCurr-tm_mon+1;   strpTime-nSec=strpCurr-tm_sec;   strpTime-nMin=strpCurr-tm_min;   strpTime-nHour=strpCurr-tm_hour。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/198133.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-04 07:30
下一篇 2024-12-04 07:30

相關推薦

  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28
  • Python基礎語言

    Python作為一種高級編程語言擁有簡潔優雅的語法。在本文中,我們將從多個方面探究Python基礎語言的特點以及使用技巧。 一、數據類型 Python基礎數據類型包括整數、浮點數、…

    編程 2025-04-28

發表回復

登錄後才能評論