C语言中常见的C的标准库函数

来源:互联网 发布:华为网络机柜 编辑:程序博客网 时间:2024/05/29 12:21

1:stdlib.h中的声明的整形算术,随机数函数和转换函数

  • int abs(int value); long abs(long value); //返回指定数值的绝对值
  • int rand(void);返回0和RAND_MAX(至少32767)之间的伪随机数
  • void srand(unsigned int seed); //避免每次运行获取相同的随机数序列,种子尽量是唯一的
  • double drand48(void); //返回0—1之间的double类型的随机数
  • int atoi(char const *string); //字符串数字转换为int类型
  • long atol(char const *string);//将字符串数字转换成long类型
  • double atof(char const *string);//将字符串数字转换成double类型

2:math.h中声明的常用的函数,注意在编译的过程中一定要去链接标准库 -lm

  • double sin(double angle); //正弦函数
  • double cos(double angle); //余弦函数
  • double tan(double angle); //正切函数
  • double asin(double angle); //反正弦函数
  • double acos(double angle); //反余弦函数
  • double atan(double angle); //反正切函数
  • double exp(double x);返回e的x次幂
  • double log(返回x以e位底的对数,即自然数);
  • double log10(double x),返回x以10为底的对数
  • double pow(double x, double y)//返回x的y次方
  • double sqrt(double x);//返回x的平方根
  • double floor(double x) //底数,1.6取出来是1
  • double ceil(double x) //顶数1.6取出来是6

3:time.h中声明的时间日期函数,time_t是无符号unsigned int整形

  • time_t time(time_t *date_time);//设置时间,从1970年1,1,0:0开始
  • char *ctime(const time_t *clock);//将时间转换成一个字符数组

主要在这里讲一下Time的使用吧,其他的数学计算相对来说都比较简单。通过一个定时器的代码来展示一下time的使用吧;

#include<stdio.h>#include<time.h>#include<stdlib.h>int main(int argc,char*argv[]){    if(argc!=2){        printf("usage:%s<number>\n",argv[0]);        exit(1);    }       int duration = atoi(argv[1]);    if(duration <= 0){         printf("duration is error \n");        exit(1);    }       time_t start,current;    time(&start);    printf("start time:%s\n",ctime(&start));    do{         time(&current);    }while(current-start !=duration);    printf("end time :%s\n",ctime(&current));    return 0;}

代码可以直接run,结果就不在这显示出来了,谢谢大家的观看。欢迎持续访问。

0 0