C语言中一些不常用函数

time
这个用的最多就是随机数的时候,现在用一用其他特性.

#include <stdio.h>
#include <time.h>

int main()
{
    time_t sec;       ///typedef long time_t
    struct tm * curTime;

    sec = time(NULL);           ///获取时间,从1970.1.1到现在的秒数,也可以写成 time(&sec);
    curTime = localtime(&sec);  ///把sec转换为当地时间,存于时间结构体curTime中,注意返回值是struct tm* 类型的

    printf("asctime(curTime): %s\n", asctime (curTime));
        ///asctime(struct tm*)函数将curTime转化成标准ASCII时间格式

    printf("ctime(curTime):   %s\n", ctime (&sec));
        ///ctime(time_t*)直接将sec转换成标准时间格式

    printf("%d-%d-%d %d:%d:%d\n", 1900+curTime->tm_year, 1+curTime->tm_mon,
        curTime->tm_mday, curTime->tm_hour, curTime->tm_min, curTime->tm_sec);
        ///手动输出struct tm中的字段,其中月份加1,年份要加1900,
        ///sec记录的是1970到现在的秒数,为什么是加1900呢???

    printf("\nsec = %ld\nmktime(curTime) = %ld\n", sec, mktime(curTime));
        ///time_t mktime(struct tm*)将curTime反向转化成1970到现在的秒数
    return 0;
}

字符转换/数学公式
#include <stdlib.h>
把字符串转化成浮点数:double atof( const char *str );
或 double strtod( const char *start, char **end );
把字符串转化成整型数:int atoi( const char *str );
长整型:long atol( const char *str );
二分查找:
void bsearch(const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void , const void *));

#include <math.h>
以十为底的对数函数:double log10(double x);
以e(=2.71828)为底:double log(double x);
反正切函数:double atan(double x); //返回值范围(-PI/2,PI/2)
double atan2(double y, double x); //返回点(x,y)与原点的连线和x轴正方向的夹角(弧度制),范围(-PI,PI]
说明: 1.特别注意:参数的顺序是y,x,不是x,y.
2.结果为正表示从x 轴逆时针旋转的角度,结果为负表示从x 轴顺时针旋转的角度.
3.atan2(y, x) 与 atan(y/x) 返回值的范围不同.
4.当x=0,y=0时返回错误.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值