看看刚开始我写的蹩脚的程序
更简介的做法!
#include <stdio.h>
2 #include <time.h>
3 int main(int argc, char *argv[])
4 {
5 time_t TimeA;
6 char *TimeString;
7 struct tm *tmA;
8 TimeA = time((time_t *)NULL);
9 tmA = localtime((time_t *)NULL);
10 TimeString = ctime(&TimeA);
11 printf("%d\n",TimeA);
12 printf("%s\n",TimeString);
13 }
后来 google了一下老外的用法,感觉自己真是太逊了
原来time()函数生成的时间是储存在参数里面的指针,并非仅仅是作为返回值输出
而且用返回值给变量也多此一举,更好的办法是直接输出,我估计这也是设计这函数本人的意思。
更简介的做法!
1 #include <stdio.h>
2 #include <time.h>
3 int main(int argc, char *argv[])
4 {
5 time_t TimeA=time((time_t *)NULL);
6 printf("%d\n",TimeA);
7 printf("%s\n",ctime(&TimeA));
8 }
参考http://www.tutorialspoint.com/c_standard_library/c_function_ctime.htm
Declaration
Following is the declaration for ctime() function.
char *ctime(const time_t *timer)
Parameters
-
timer -- This is the pointer to a time_t object that contains a calendar time.
Return Value
This function returns a C string containing the date and time information in a human-readable format.
Example
The following example shows the usage of ctime() function.
#include <stdio.h> #include <time.h> int main () { time_t curtime; time(&curtime); printf("Current time = %s", ctime(&curtime)); return(0); }
Let us compile and run the above program, this will produce the following result:
Current time = Mon Aug 13 08:23:14 2012