110-C language global variables and local variables

Global variables: variables defined outside the function (use as little as possible, unsafe)
local variables: variables defined inside the function, including formal parameter
link attributes: for multiple files, whether the symbols (variables, functions) of one file can be used by others Seen in
the file Header file: store the declaration of the function
Source file: store the implementation of the function

Introduction to local variables

#include<stdio.h>

void Fun()
{
    
    
	static int a = 0;//第一次定义时执行,以后再来时不执行,考试重点
	a++;
	printf("%d\n",a);
}
int main()
{
    
    
	//int a =  10;
	//printf("%d\n",a);
	for(int i=0;i<10;i++)
	{
    
    
		Fun();
	}
	return 0;
}

The results of the operation are as follows
Insert picture description here
:

#include<stdio.h>

//全局变量介绍
int g_a = 10;//全局变量,从定义开始直到文件结尾都能使用
int g_b;//全局变量未初始化,系统自动初始化为0
//extern int  g_c;//引用外部符号
//extern int g_d;

void Fun()
{
    
    
	printf("%d\n",g_a);
}

int main()
{
    
    
	printf("%d\n",g_a);
	Fun();
	printf("%d\n",g_b);

//	printf("%d\n",g_c);
//	printf("%d\n",g_d);

	return 0;
}

The results are as follows
Insert picture description here

Guess you like

Origin blog.csdn.net/LINZEYU666/article/details/111566143