专业编程基础技术教程

网站首页 > 基础教程 正文

库函数——ctime()和ctime不安全函数

ccvgpt 2024-10-19 03:25:43 基础教程 17 ℃

声明:char *ctime(const time_t *timer)

timer:这是指向time_t对象的指针,该对象包含一个日历时间

库函数——ctime()和ctime不安全函数

#include<stdio.h>

#include<time.h>

int main(){

time_t curtime;//定义对象curtime;

time(&curtime);//用time函数将对象curtime转换指针

printf("当前时间=%s",ctime(&curtime));//返回当前时间

return 0;

}

ctime不安全函数:

errno_t ctime_s(char *buffer,rsize_t bufsz,const time_t *time)

#include<stdio.h>

#include<time.h>

int main(){

time_t curtime=time(null);

char str[26];

ctime_s(str,sizeof str,&curtime);//对象,变量字节大小,指针对象。

printf(str);

}

int main(void) {

time_t now = time(0);

char str[26];

ctime_s(str, sizeof str, &now);

cout << str << endl;

cout << now << endl;

struct tm tml;

localtime_s(&tml,&now);

cout << "年:" << tml.tm_year+1900 << endl;

cout << "月:" << tml.tm_mon+1<< endl;

cout << "日:" << tml.tm_mday << endl;

cout << "时间:" << tml.tm_hour << ":" << tml.tm_min << ":" << tml.tm_sec << endl;

cout << tml.tm_wday << endl;

cout << tml.tm_yday+1 << endl;

system("pause");

return 0;

}

Tags:

最近发表
标签列表