当前位置:网站首页>Local variables and global variables in C language

Local variables and global variables in C language

2022-06-25 20:31:00 @sen

What is defined in a function is called local variable ; What is defined outside the function is called External variables , Also called All variables

#include <stdio.h>
int main()
 {
	int i = 520;         // here i Is a global variable 
	printf("before i=%d\n", i);  

	for (i = 0; i < 10; i++)
	{
		printf("i=%d\n", i);  // Redefinition i Value 
	}

	printf("after i=%d", i);   // Or use the values defined by global variables 
	return 0;
}

stay c99 in , Local variables do not affect global variables , Global variables can be shared by other functions of this program , If the global variable is not initialized , Then it will automatically initialize to 0

#include<stdio.h>

void a();
void b();
void c();

int count = 0;  // Global variables 

void a()
{
	count++; // Add one to the global variable 
}
void b()
{
	count++;   // Add one to the global variable 
}
void c()
{
	count++;   // One more 
}

int main()
{
	a();  // Call the global variable once 
	b();  // Add one 
	c();  //  Add one 
	b();  // Add one 
	printf("%d\n", count);   //%d=4
}

If there is a local variable with the same name as the global variable inside the function , The compiler does not report errors , Instead, mask global variables in functions ( That is, in this function , Global variables don't work )

Here's an example

#include<stdio.h>

int a, b = 520;

void func()   // Defined function c
{
	int b;
	a = 880;
	b = 120;
	printf("In func,a=%d,b=%d\n", a, b);
}

int main()
{
	printf("In main,a=%d,b=%d\n", a, b);
	func();                             // Changed local variables , So here a=880,b=120
	printf("In main,a=%d,b=%d\n", a, b);  
       //func The local variables changed inside , Does not affect global variables , But because of a Not initialized , So it was changed , but b Is still 520

		return 0;
}

count After the definition of the function, define An error will be reported count Undeclared identifier , This requires extern keyword
  extern The keyword tells the compiler : I defined this variable later , Don't rush to report mistakes

#include<stdio.h>

void func();

void func()
{
	extern int count;  // Used extern keyword , It is compiled successfully 
	count++;
}
int count = 20;  

int main()
{
	func();
	printf("%d\n", count);
	return 0;
}

It's best not to use global variables too much !! But sometimes it's useful

Here's why :1、 Using global variables will make your program take up more memory , Because global variables start when they are defined , It is not released until the program exits
                 2、 Suddenly the namespace , Although local variables mask global variables , But this will also reduce the readability of the program , It is often difficult to judge the meaning and scope of each variable
                  3、 The coupling of the program is improved , Pull one hair and move the whole body , Time is long. , The code is long , You may not know which functions have modified the global variables

原网站

版权声明
本文为[@sen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202181347464905.html