<> function

c Language is a process oriented high-level language

open VS You can see the header file of the encapsulated library function in the installation path .h And its implementation .c file c, In fact, some library functions encapsulate system call functions , Call the library function to call the system call indirectly

Naku function strlen Let's have a chestnut ,strlen Is a function that calculates the number of characters in a string , be located string.h Header file
#include <stdio.h> #include <string.h> int main() { char str[] = "producing!";
printf("%d", strlen(str)); return 0; }
Let's simulate it strlen

Recursive implementation strlen
// Recursive implementation strlen #include <stdio.h> int strlen_self(char* ch) { if (*ch == '\0')
return 0; return 1 + strlen_self(ch + 1); } int main() { char st[10] =
"ASDFGHJKL"; printf("%d\n", strlen_self(st)); return 0; }
Non recursive implementation strlen
// Non recursive implementation strlen #include <stdio.h> int strlen_self(char* ch) { int sum = 0; while
(*ch++ != '\0') { sum++; } return sum; } int main() { char st[10] = "ASDFGHJKL";
printf("%d\n", strlen_self(st)); return 0; }
Library function is a mature and safe function which is packaged by human and used by program developers , The time cost of repeated wheel building is avoided , It improves the efficiency of program development and shortens the time cycle

<> recursion

The programming skill of program call itself is called recursion ( recursion). Recursion is an idea of divide and conquer , A large-scale series of jobs with the same operation are divided into several small jobs of the same operation to simplify the steps .
<> Two necessary conditions of recursion :
*
There are limitations , When this constraint is met , Recursion does not continue .

*
After each recursive call, it gets closer to this constraint .

Recursive simulation from above strlen Function starting of function , We analyze the process of recursion

In order to facilitate observation and tracking, understand the recursive process , We will st Initialize to string "ASD"
// Recursive implementation strlen #include <stdio.h> int strlen_self(char* ch) { if (*ch == '\0')
return 0; return 1 + strlen_self(ch + 1); } int main() { char st[4] = "ASD";
printf("%d\n", strlen_self(st)); return 0; }
We can see from the function stack frame ,strlen_self Function called in total 4 second , The process of stack frame out of stack is the process of recursive return

Keep coming out of the stack …

Until there is only one left in the stack main The frame of the function

Will be in main Function strlen_self Return value of

Technology
©2019-2020 Toolsou All rights reserved,
How to prevent Mac put to sleep ? Tell you 4 This is an effective way be based on C# Students' grades / mis solve element-ui The drop-down box of has a value but cannot be selected Student information management system source code -C language VR,AR and MR The difference between these technologies 【 Data structure and algorithm 6】 Stack implementation of comprehensive calculator utilize python Yes monkey Automatic log analysis Laya Wechat games sub package loading Alibaba enterprise email smtp set up ( practice )【Java Details of knowledge points 3】 Serializable and Deserialize