C language static keyword parameters

static keyword.

1. Static modifies local variables;

① The entire life cycle is extended,
② The static local variable will only be initialized once, and every time the static local variable is called in the future, the value saved after the last call will be used.
③ It can only be accessed by variables and functions in the scope. Although it exists in the entire life cycle of the program, it cannot be accessed by other functions or files.

//没有static修饰变量
#include<stdio.h>
#include<windows.h>
int func(int c){
    
    
	int a = 0;
	int b = 3;//修饰
	int sum;
	a++;
	b++;
	sum =a + b + c;
	return sum;
}
int main()
{
    
    
	int c = 1;
	for (int i = 1; i <= 4;i++){
    
    
		printf("%d\n",func(c));
	}
	system("pause");
	return 0;
}
//输出结果为6
6
6
6
请按任意键继续. . .
static修饰变量;

#include<stdio.h>
#include<windows.h>
int func(int c){
    
    
	int a = 0;
	static int b = 3;
	int sum;
	a++;
	b++;
	sum =a + b + c;
	return sum;
}
int main()
{
    
    
	int c = 1;
	for (int i = 1; i <= 4;i++){
    
    
		printf("%d\n",func(c));
	}
	system("pause");
	return 0;
}
//输出结果为:
6
7
8
9
请按任意键继续. . .·


2. Static modifies global variables

Global variables are modified by static. Static global variables can only be used in the source file where they are defined, and are not visible in the source file of the same project.

//test1.c源文件
#include<stdio.h>
#include<windows.h>
int a = 1;
//我们在test1.c文件文件中定义一个整型变量,在test2c中使用

//test2源文件
#include<stdio.h>
#include<windows.h>
int main(){
    
    
	extern int a;
	printf("%d\n",a);
	system("pause");
	return 0;
}
//运行结果为
1
请按任意键继续. . .

//运行就会报搓

Ordinary global variables are visible to the entire project, and other files can be used directly after being declared externally by extern

The following uses static modification for the variables in the above example

//test1.c文件
#include<stdio.h>
#include<windows.h>
static int a = 1;

//test2.c文件
#include<stdio.h>
#include<windows.h>
int main(){
    
    
	extern int a;
	printf("%d\n",a);
	system("pause");
	return 0;
//运行结果会报错
//1>LINK : fatal error LNK1168: 无法打开 D:\c 语言\VS //code\static\Debug\static.exe 进行写入========== 生成:  成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

3. Static modified function

Static modified function, the function is invisible to other source files except this source file. When the function does not want to be seen by the outside world, use static to modify this function.

//test1.c文件
#include <stdio.h>
#include<windows.h>
void fun(void)
{
    
    
	printf("hello\n");
	system("pause");
}


//test2.c文件
#include <stdio.h>
#include<windows.h>
int main(void)
{
    
    
	fun();
	system("pause");
	return 0;
}
//运行结果为:
hello
请按任意键继续. . .

The function is not decorated with static, the function can be called by other files.
Below we use static to modify the function in the above example

//test1.c文件
#include <stdio.h>
#include<windows.h>
 static void fun(void)
{
    
    
	printf("hello\n");
	system("pause");
}

//test2.c文件
#include <stdio.h>
#include<windows.h>
int main(void)
{
    
    
	fun();
	system("pause");
	return 0;
}
//运行就会报错
//1>D:\c 语言\VS code\static\Debug\static.exe : fatal error LNK1120: 1 个无法解析的外部命令
//此时的fun()函数只能在test1.c文件内使用,test2.c是看不见test1.c文件内的fun()函数

Guess you like

Origin blog.csdn.net/supermanman_/article/details/109139790