ansi c语言编写api,C语言学习——ANSI C标准函数库

即C语言环境自带的变量和方法等

stdioh

getchar和putchar

gets和puts

sprintf和sscanf

stringh

strcpy

strlen

strtok

stdlibh

rand

srand

timeh

time_t

timeNULL

tm

localtime

案例1求当前的年月日时分秒

mktime

案例2求两个日期之间的日数差

stdio.h

getchar和putchar

前者或者控制台输入的字符

后者输出字符

例如:

char c;

while((c=getchar())!='\n'){

putchar(c);

}

gets和puts

输入和输出字符串

例如:

char cs[10];

gets(cs);

puts(cs);

sprintf和sscanf

前者为格式化字符串。后者为提取字符串中的数字

char buf1[128];

sprintf(buf1,"NAME:%s,Age:%d","zwq",21);

//格式化字符串。参数:要写入的字符串变量,字符串格式,字符串值

printf("%s\n",buf1);

char* ch1=(char*)malloc(128);

sprintf(ch1,"NAME:%s,Age:%d","zwq",21);

//第一个参数也可为指针

printf("%s\n",ch1);

//sscanf用于提取数字,注意是数字

const char buf2[]="2016-09-26";

int year,month,day;

int count=sscanf(buf2,"%d-%d-%d",&year,&month,&day);

//count表示提取到几个变量

if(count==3){

printf("当前年为%d,当前月为%d,当前日为%d\n",year,month,day);

}else{

printf("获取数据出错\n");

}

注:如果要提取字符串中的数字之外的数据则需要使用字符串的分割

string.h

strcpy

赋值字符串

两个参数:第一个参数要赋值的变量,第二个参数赋的值

strlen

字符串长度

一个参数:所求的字符串

strtok

分割字符串

例如:

char str[] = "now # is the time for all # good men to come to the # aid of their country";

char delims[] = "#";

char *result = NULL;

result = strtok( str, delims );

while( result != NULL ) {

printf( "result is \"%s\"\n", result );

result = strtok( NULL, delims );

}

stdlib.h

rand();

返回随机数 缺点:伪随机数,只能随便一次执行的执行次数生成随机数,例如重复执行生成随机数的程序,得到的结果是一样的

srand();

设置随机数序列,默认为srand(1);参数为任何一个int,不同的序列会产生不同的随机数

例如

srand(time(NULL));

for(int i=0;i<10;i++){

printf("%d\n",rand());

}

注:一般需要随机数在某个区间,这个时候需要知道的是:取余一个数,则不可能超过这个数

例如:随机生成 0~10:rand()*10;

随机生成1~10:rand()*9+1

time.h

time_t

时间格式变量

time(NULL)

返回当前时间与1970-01-01 00:00:00的时间差(单位为秒)

tm

时间结构体

struct tm

{

int tm_sec;//秒

int tm_min;//分

int tm_hour;//时

int tm_mday;//日

int tm_mon;//月

int tm_year;//年

int tm_wday;//周几:~6,表示周一

int tm_yday;//当前年的第几天

}

localtime

将秒数转化为tm格式

案例1:求当前的年月日时分秒

time_t now=time(NULL);

//把now(单位为秒)转化成时间格式

tm info= *localtime(&now);

int year=info.tm_year + 1900;

int month=info.tm_mon +1;

int day=info.tm_mday;

int hour=info.tm_hour;

int minute=info.tm_min;

int second=info.tm_sec;

printf("year==%d\nmonth==%d\nday==%d\nhour==%d\nminute==%d\nsecond==%d",

year,month,day,hour,minute,second);

mktime

将一个tm对象(结构体)转换成time_t格式

案例2:求两个日期之间的日数差

time_t convert(int year,int month,int day,int hour,int min,int sec)

{

tm info={0};

info.tm_year=year-1900;

info.tm_mon=month-1;

info.tm_mday=day;

info.tm_hour=hour;

info.tm_min=min;

info.tm_sec=sec;

time_t t=mktime(&info);

return t;

}

time_t start=convert(2016,9,28,0,0,0);

time_t end=convert(2016,9,29,0,0,0);

int secs=(int)(end-start);

int days=secs/(24*3600);

以上是总结常用的,具体的可以参考

http://download.csdn.net/download/zhengyikuangge/9642061

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值