Personal homepage: Click me to enter the homepage
Column classification: C language elementary C language programming——KTV C language mini-game C language advanced
Welcome everyone to like, comment, and collect.
Work hard together and go to Dachang together.
Table of contents
1.Library functions
The library functions I mainly explain today include pow function, sqrt function, strcpy function, strlen function, memset function, strcmp function.
1.1pow function
The header file of the pow function is #include<math.h>, and the function of the pow function is to get the b power of a. For example, we want to get 2 to the 5th power, the code is as follows:
#include <stdio.h>
#include <math.h>
int main()
{
int a;
a = (int)pow(2, 5);
printf("%d", a);
return 0;
}
The result of running is as follows
1.2sqrt function
The header file of the sqrt function is #include<math.h>, and the function of the sqrt function is to get the quadratic of a certain number. For example, we want to get the root number 4, the code is as follows:
#include <stdio.h>
#include <math.h>
int main()
{
int a;
a = sqrt(4);
printf("%d", a);
return 0;
}
The results are as follows
1.3strcmp function
The header file of the strcmp function is #include<string.h>. The function of the strcmp function is to compare strings. If it is greater, it returns 1, if it is less than, it returns -1, and if it is equal, it returns 0; the code is as follows:
#include <stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcdef";
char brr[] = "abc";
char crr[] = "abc";
printf("大于时返回%d\n", strcmp(arr, brr));
printf("小于时返回%d\n", strcmp(brr, arr));
printf("等于时返回%d\n", strcmp(brr, crr));
return 0;
}
The running results are as follows:
1.4strcpy function
The header file of the strcpy function is #include<string.h>, the function of the strcpy function is to copy a string to another string; the code is as follows:
#include <stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcdef";
char brr[] = "abc";
printf("%s\n", strcpy(arr, brr));
return 0;
}
When we enter the debugger, we can see that the arr array copies the brr array, and '\0' is also copied.
1.5strlen function
The header file of the strlen function is #include<string.h>, the function of the strlen function is to calculate the length of the string; the code is as follows:
#include <stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcdef";
printf("%d\n", strlen(arr));
return 0;
}
The running results are as follows:
1.6memset function
The header file of the memset function is #include<string.h>, and the function of the strlen function is to change part of the string; the code is as follows:
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "abcdefg";
printf("%s", memset(arr, 'A', 5));
return 0;
}
The running results are as follows:
2. Custom functions
Custom functions can be divided into passing by value and passing by address;
2.1 Passing value
Pass-by-value types will not modify the actual parameters. They only need to return a value and modify the formal parameters. For example, to find the maximum value of two numbers, the code is as follows:
#include <stdio.h>
int max(int x, int y);//此处是声明定义的函数
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("max=%d", max(a, b));
}
int max(int x, int y)//自定义函数
{
if (x > y)
return x;
else
return y;
}
The running results are as follows:
2.2 Address transfer
A pass-by type is one that can modify the value of an actual parameter by address. For example, the code for exchanging the values of a and b is as follows:
#include<stdio.h>
void swap(int* p, int* q);
int main()
{
int a, b;
scanf("%d%d", &a, &b);
swap(&a, &b);
printf("a=%d\nb=%d", a, b);
}
void swap(int* p, int* q)
{
int flag;
flag = *p;
*p = *q;
*q = flag;
}
The running results are as follows:
2.3 Analysis
When running a custom function in a custom function, space will be opened in the stack area. When the custom function ends, the memory will be released, so it will be divided into pass-by-value and pass-by-address.