说明:字符串(即字符数组)在程序开发中使用很是多,经常使用的函数须要掌握程序员
获得字符串的长度 size_t strlen(const char *str)编程
拷贝字符串 char *strcpy(char *dest,const char *src)数组
链接字符串 char *strcat(char *dest,const char *src)函数
#include <stdio.h> #include <string.h> //头文件中声明字符串相关的系统函数 void main(){ char src[50] = "abc",dest[50]; //定义两个字符数组(字符串),大小为50 char *str = "abcdef"; printf("str.len=%d",strlen(str));//统计字符串的大小 //表示将“hello”拷贝到src //注意:拷贝字符串会将原来的内容覆盖 strcpy(src,"hello"); printf("src=%s",src); strcpy(dest,"尚硅谷"); //strcat是将src字符串的内容链接到dest,可是不会覆盖dest原来的内容,而实链接 strcat(dest,src); printf("最终的目标字符串:dest=%s",dest); getchar(); }
说明:在编程中,程序员会常用到日期相关的函数,好比:统计某段代码花费的时间,头文件是<time.h>code
获取当前时间 char *ctime(const time_t *timer)开发
编写一段代码来统计函数test执行的时间字符串
#include<stdio.h> #include<time.h>//该头文件中,声明和日期和时间相关的函数 void test(){ //运行test函数,看看花费多少时间 int i=0; int sum=0; int j=0; for(i=0;i<777777777,i++){ sum=0; for(j=0;j<10;j++){ sum+=j; } } } int main(){ time_t curtime;//time_h是一个结构体类型 time(&curtime);//time()完成初始化 //ctime返回一个表示当地时间的字符串,当地时间是基于参数timer printf("当前时间=%s",ctime(&curtime)); getchar(); return 0; //先获得执行test前的时间 time_t start_t,end_t; double diff_t;//存放时间差 printf("程序启动..."); time(&start_t);//初始化获得当前时间 test(); //再获得test后的时间 time(&end_t);//获得当前时间 diff_t=difftime(end_t,start_t);//时间差,按秒ent_t - start_t //而后获得两个时间差就是耗用的时间、 printf("执行test()函数耗用了%.2f秒",diff_t); getchar(); return 0; }
math.h 头文件定义了各类数学函数和一个宏,在这个库中全部可用的功能都带有一个double类型的参数,且都返回double类型的结果get
#include <stdio.h> #include <math.h> void main(){ double d1=pow(2.0,3.0); double d2=sqrt(5.0); printf("d1=%.2f",d1); printf("d2=%.2f",d2); getchar(); }