当前位置:网站首页>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
边栏推荐
- 2020-11-14-Alexnet
- Leetcode daily question - 28 Implement strstr() (simple)
- This is a simple and cool way to make large screen chart linkage. Smartbi will teach you
- What is the core journal of Peking University? An article will help you understand it thoroughly
- <C>. Figure guessing game
- Avoid material "minefields"! Play super high conversion rate
- Curtain down and departure
- Slenium tips: how to handle some dialog boxes that may appear on Web pages
- Understand the offline mixing technology in the industry
- Leetcode theme [array] -31- next spread
猜你喜欢

Redis core principle and design idea

Jmeter+grafana+influxdb build visual performance test monitoring platform (full)

Huawei fast application access advertising service development guide
R language momentum and Markowitz portfolio model implementation

Several methods of obtaining function annotation text on pycharm
[distributed system design profile (1)] raft

<C>. array

How does pycharm create multiple console windows for debugging in different environments?

Connect the local browser to the laboratory server through mobaxterm

Impact of Huawei application transfer and application claim on user identification
随机推荐
String since I can perform performance tuning, I can call an expert directly
Intra domain information collection for intranet penetration
PIP command -fatal error in launcher: unable to create process using How to resolve the error after migrating the virtual environment?
How does zhiting home cloud and home assistant access homekit respectively? What is the difference between them?
Share a billing system (website) I have developed
C language PTA -- continuity factor
String since I can perform performance tuning, I can call an expert directly
Day 28/100 CI CD basic introductory concepts
Interview records
Interviewer: why does TCP shake hands three times and break up four times? Most people can't answer!
Avoid material "minefields"! Play super high conversion rate
How to play one to many in JPA?
8. iterators and generators
Solution to big noise of OBS screen recording software
Attention to government and enterprise users! The worm prometei is spreading horizontally against the local area network
TypeError: __ init__ () takes 1 positional argument but 5 were given
206. reverse linked list (insert, iteration and recursion)
The secret of metaktv technology of sound network: 3D space sound effect + air attenuation + vocal blur
2.4 finding the sum of the first n terms of the interleaved sequence
<C>. Calculation date to day conversion