Principle
Static in the C language can be used to change the scope and lifetime of a variable and the scope of a function, a keyword that modifies the definition and declaration of a function, and the definition of a variable.
The function is defined with the static modifier, which indicates that the function is only valid in this file (the file in which it is defined) and other files are not visible to the function.
A variable defined outside the static modifier that indicates that the variable is valid only in this file (the file in which it is defined) and that the other file is not visible to the variable.
The variable definition in the static modifier function indicates that the variable has been valid for multiple function calls. Its scope is still a function, but the lifetime is the lifetime of the entire program
Declared with the static modifier function, which means that the definition of the function can only be used in this file
About declarations and definitions
If the definition precedes use, you do not need to declare
When defined and used, declare before use
Small experimental File1.cpp
#include <stdio.h>//static variable,used only in a file static int a;//static function,used only in a filestatic void F (void) {a=1;printf ("a=%d\n", a), a=2;printf ("a=%d\n", a);} void ff (void) {f ();}
Main.cpp
#include <stdio.h>//in "F1.cpp", define a and F as static int a;void f (void) {printf ("f () \ n");} void ff (void), extern void print (void),/*************************** main function ***************************/int main () {print ( );p rint ();p rint (), FF ();p rintf ("a=%d\n", a); F (); return 0;} void print (void) {static int n=0; if (n==0) {printf ("the first time\n"); n=1;} else{printf ("Not the first time\n");} printf ("The Address of N is:%x\n", &n);}
Experimental results
Static in the C language