some commonly used functions

some commonly used functions

1. Commonly used system functions in strings

header file <string.h>

1. Get the length of the string

size_t strlen(const char *str)

Computes the length of the string str up to but not including the null-termination character.

2. Copy the string

char strcpy(char dest, const char *src)

Copy the string pointed to by src to dest .

3. Connection string

char strcat(char dest, const char *src)

Appends the string pointed to by src to the end of the string pointed to by dest .

4. Code demo

#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();
}

image-20221025201733570

2. Time and date related functions

header file <time.h>

(1) Get the current time

char *ctime(const time_t *timer)

Returns a string representing the local time, based on the parameter timer.

(2) Write a piece of code to count the execution time of the function test

double difftime(time_t time1, time_t time2)

Returns the difference in seconds between time1 and time2 (time1-time2)

image-20221025202935690

3. Mathematics-related functions

The math.h header file defines various math functions and a macro. All functions available in this library take a parameter of type double and return a result of type double

for example:

(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 的绝对值。

Code demo:

image-20221025203437918

Guess you like

Origin blog.csdn.net/m0_53415522/article/details/127521402