<> 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,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?