c语言常用的系统函数
字符串常用的系统函数
1.字符串常用函数的头文件
<string.h>
2.得到字符串的长度
size_t strlen(const char *str)
计算字符串 str 的长度,直到空结束字符,但不包括空结束字符。
3.拷贝字符串
char *strcpy(char *dest, const char *src)
把src所指向的字符串复制到dest。
4.连接字符串
char *strcat(char *dest, const char *src)
把src所指向的字符串追加到dest所指向的字符串的结尾。
案例
#include<stdio.h>
#include<string.h>
void main(){
//定义了两个字符串,大小为50
char src[50]="abc";
char dest[50];
char *str ="abcdef";
printf("\n str.len=%d",strlen(str));// strlen 是用来统计字符串大小
strcpy(src,"hello");//将“hello 拷贝到 src
//拷贝字符串会将原来字符串覆盖
printf("\n src=%s",src);
strcpy(dest,"云云豆酱");
//strcpy 是将 src 字符串的内容链接到 dest ,不会覆盖原来的内容
strcat(dest,src);//hello云云豆酱
printf("\n 最终的目标字符串:| %s |",dest); //用 | |来标志输出的内容
}
时间与日期常用的系统函数
1.头文件
<time.h>
1.获取当前时间
char *ctime(const time_t *timer)
返回一个表示当地时间的字符串,当地时间是基于参数timer
2.编写一段代码来统计函数test执行的时间
double difftime(time_t time1 , time_t time2)
返回timel和 time2之间相差的秒数(timel-time2)。
1.获取当前时间
2.编写一段代码来统计函数test执行的时间
#include<stdio.h>
#include<time.h>
//统计改函数运行花费的时间
void test(){
int i = 0,sum = 0,j = 0;
for(i=0;i<9999999;i++){
sum=0;
for(j=0;j<100;j++){
sum +=j;
}
}
}
void main(){
time_t start_t;//存放开始时间
time_t end_t; //存放结束时间
double diff_t;//存放时间差
//先得到执行 test 的时间
time(&start_t);//得到当前时间
test();//执行 test 函数
//得到执行 test 后的时间
time(&end_t);//得到当前时间
//用执行后时间 - 执行前的时间
diff_t = difftime(end_t,start_t);
printf("test函数运行时间:%.2f秒",diff_t);
}
数学常用的系统函数
1.头文件
math.h
定义了各种数学函数和一个宏。
在这个库中所有可用的功能都带有一个double类型的参数,且都返回double类型的结果。
2.返回e的x次幂的值。
double exp(double x)
3.返回x的自然对数(基数为e的对数)
double log(double x)
4.返回x的y次幂。
double pow(double x, double y)
5.返回x的平方根。
double sqrt(double x)
6.返回x的绝对值。
double fabs(double x)
案例 4返回x的y次幂。5返回x的平方根。 6返回x的绝对值。
#include<stdio.h>
#include<math.h>
void main(){
double d1 = pow(2.0,3.0);
printf("\n %.2f",d1);
double d2 = sqrt(5.0);
printf("\n %.2f",d2);
double d3 = fabs(-5.0);
printf("\n %.2f",d3);
}