Des*_*tor -2 c printf strftime format-specifiers
我从这里学习C语言中与时间相关的函数.他们使用以下示例演示了strftime()函数:
#include <stdio.h>
#include <time.h>
#define LEN 150
int main ()
{
char buf[LEN];
time_t curtime;
struct tm *loc_time;
//Getting current time of system
curtime = time (NULL);
// Converting current time to local time
loc_time = localtime (&curtime);
// Displaying date and time in standard format
printf("%s", asctime (loc_time));
strftime (buf, LEN, "Today is %A, %b %d.\n", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "Time is %I:%M %p.\n", loc_time);
fputs (buf, stdout);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对printf()中的%m说明符进行了调整.它说%m转换说明符不是C,而是printf的GNU扩展.'%m'转换在errno中打印对应于错误代码的字符串.
我知道格式说明符%是C99中的新内容.它以十六进制形式打印浮点数.
但是这个程序中%b和%I的目的是什么?我不明白%b&%I的用途是什么?我从来没有听说过这个.%I与%i相同吗?
strftime()格式与格式无关sprintf()
.它们都使用%符号和字母,但这就是相似性的结束.整个(和唯一的)使用点strftime()是控制打印日期/时间值的格式,就像printf()用来控制打印数字和字符串数据的格式一样.因为它们完全不同在两个函数中,获得不同的结果(以及重用有限的字母表来表示不同的东西)是合理的.
在strftime():
%A 是星期几的完整本地名称.%b 是缩写的月份名称.%d 是这个月的日子.%I 是12小时格式的小时.%M 是分钟.%p 是AM/PM指标.其中%b,%I并且%M在标准中没有任何意义printf(),而:
%Adouble用大写字母打印十六进制表示法.%dint以十进制打印.%p打印一个void *地址.