C language: keyword ---static

Static has two functions in the C language. The first is to modify variables , and the second is to modify functions .


1. Static modified variable

According to the different scope, variables are divided into local variables and global variables. If you use static to modify a variable, regardless of whether the variable is global or local, it is stored in the static data area .

1.1 Local variables

  • Ordinary local variables: Variables
    defined in any function (without static modifier) ​​belong to this category. The compiler generally does not initialize ordinary local variables, which means that its value is uncertain at the beginning, unless it is explicitly assigned.

    Ordinary local variables are stored in the process stack space and will be released immediately after use.

  • Static local variable:
    The local variable defined with the static modifier will be initialized to 0 by the compiler even if the initial value is not assigned in the declaration.

    Static local variables are stored in the static data area of ​​the process. Even if the function returns, its value will remain unchanged.

The specific procedures are as follows:

#include <stdio.h>

void example(void)	
{
    
    
	int n=10;
	static int m=10;
	
	printf("n=%d\n", n);
	n++;
    printf("n++=%d\n", n);
	
	printf("m=%d\n", m);
	m++;
    printf("m++=%d\n", m);
}
int main(void)
{
    
    
	example();
	printf("--------------------\n");
	example();
	printf("--------------------\n");
	example();
	return 0;
}

The results are as follows:

n=10
n++=11
m=10
m++=11
--------------------
n=10
n++=11
m=11
m++=12
--------------------
n=10
n++=11
m=12
m++=13
请按任意键继续. . .

1.2 Global variables

Global variables are defined outside the function body, storage space is allocated in the global data area, and the compiler will automatically initialize them.

  • Common global variables : Visible to the entire project, other files can be used directly after being declared externally by extern. In other words, other files can no longer define a variable with the same name (otherwise the compiler will consider them the same variable).
  • Static global variables : only visible to the current file, other files are inaccessible, other files can define variables with the same name, and the two do not affect each other. When defining global variables that do not need to be shared with other files, adding the static keyword can effectively reduce the coupling between program modules, avoid conflicts of variables with the same name in different files, and prevent misuse.

2. Static modified function

The use of functions is similar to global variables. Add static before the return type of the function, which is a static function. Its characteristics are as follows:

  • A static function can only be visible in the file where it is declared, and other files cannot reference the function.
  • Different files can use static functions with the same name without affecting each other.

Guess you like

Origin blog.csdn.net/MQ0522/article/details/110850832