学习时间函数

看看刚开始我写的蹩脚的程序
 #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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值