Practical application scenarios and usage skills of static keyword in C language
1. Overview
static is a keyword in C language, used for modification variables and functions. Its function is to change its life cycle and visibility during program running, making variables and functions static. This article will introduce the practical application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples.
2. Static variables
#include <stdio.h> int recursive() { static int count = 0; count++; printf("当前递归次数:%d ", count); if (count < 5) { recursive(); } return count; } int main() { int result = recursive(); printf("递归结束,共计调用次数:%d ", result); return 0; }
Running results:
当前递归次数:1 当前递归次数:2 当前递归次数:3 当前递归次数:4 当前递归次数:5 递归结束,共计调用次数:5
It can be seen that by using the static keyword to modify the count variable, the value of the variable is maintained during the recursive call, achieving the accumulation of the number of recursions. .
// file1.c #include <stdio.h> static int global = 10; void printGlobal() { printf("file1.c中的global:%d ", global); }
// file2.c #include <stdio.h> static int global = 20; void printGlobal() { printf("file2.c中的global:%d ", global); }
// main.c #include <stdio.h> extern void printGlobal(); int main() { printGlobal(); return 0; }
Run result:
file1.c中的global:10
In this example, because the global variable is modified by the static keyword, static variables with the same name can be defined in different source files without Cause conflict.
3. Static function
// file1.c #include <stdio.h> static void privateFunc() { printf("这是file1.c中的私有函数 "); } void publicFunc() { printf("这是file1.c中的公共函数 "); privateFunc(); }
// file2.c #include <stdio.h> static void privateFunc() { printf("这是file2.c中的私有函数 "); } void publicFunc() { printf("这是file2.c中的公共函数 "); privateFunc(); }
// main.c #include <stdio.h> extern void publicFunc(); int main() { publicFunc(); return 0; }
Running results:
这是file1.c中的公共函数 这是file1.c中的私有函数
In this example, since the privateFunc function is modified by the static keyword, static functions with the same name can be defined in different source files without Cause conflict.
#include <stdio.h> void printCount() { static int count = 0; count++; printf("函数调用次数:%d ", count); } int main() { int i; for (i = 0; i < 5; i++) { printCount(); } return 0; }
Running results:
函数调用次数:1 函数调用次数:2 函数调用次数:3 函数调用次数:4 函数调用次数:5
You can see that by using the static keyword to modify the count variable, the value of the variable is maintained between multiple calls of the function, realizing the function The cumulative number of calls.
4. Summary
This article introduces the practical application scenarios and usage techniques of the static keyword in C language. By describing the examples of static variables and static functions in detail, we can find that the static keyword plays an important role in extending the life cycle of variables, controlling the visibility of variables and functions, and controlling the number of variable initializations. Reasonable application of the static keyword can improve the readability, maintainability and security of the program. I hope this article will be helpful to readers in their application of C language programming.
The above is the detailed content of Practical application scenarios and usage skills of the static keyword in C language. For more information, please follow other related articles on the PHP Chinese website!