一些常用的函数
一.字符串中常用的系统函数
头文件<string.h>
1.得到字符串的长度
size_t strlen(const char *str)
计算字符串 str 的长度,直到空结束字符,但不包括空结束字符。
2.拷贝字符串
char strcpy(char dest, const char *src)
把 src 所指向的字符串复制到 dest。
3.连接字符串
char strcat(char dest, const char *src)
把 src 所指向的字符串追加到 dest 所指向的字符串的结尾。
4.代码演示
#include <stdio.h>
#include <string.h>//头文件中声明字符串相关的系统函数
int main(){
char src[50]="abc" , dest[50];//定义了两个字符数组(字符串),大小为50
char *str = "abcdff";
printf("\nstr.len=%d",strlen(str));//统计字符串的大小
//表示将“hello”拷贝到src
//注意,拷贝字符串会将原来的内容覆盖
strcpy(src,"hello");
printf("\nsrc=%s",src);
strcpy(dest,"Genrany");
//strcat是将src 字符串的内容连接到dest,但是不会覆盖dest原来的内容,而是连接
strcat(src,dest);//"helloGenrany"
printf("\n最终的目标字符串 :dest=%s",dest);
getchar();
}
二.时间和日期相关函数
头文件 <time.h>
(1) 获取当前时间
char *ctime(const time_t *timer)
返回一个表示当地时间的字符串,当地时间是基于参数timer。
(2)编写一段代码来统计 函数test 执行的时间
double difftime(time_t time1, time_t time2)
返回 time1 和 time2 之间相差的秒数 (time1-time2)
三.数学相关的函数
math.h 头文件定义了各种数学函数和一个宏。在这个库中所有可用的功能都带有一个 double 类型的参数,且都返回 double 类型的结果
举例说明:
(1) double exp(double x)
//返回 e 的 x 次幂的值。
(2) double log(double x)
//返回 x 的自然对数(基数为 e 的对数)
(3) double pow(double x, double y)
//返回 x 的 y 次幂。
(4) double sqrt(double x)
//返回 x 的平方根。
(5) double fabs(double x)
//返回 x 的绝对值。