当前位置:网站首页>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
边栏推荐
- Robotium_ (clickbyid method)
- Transunet reading notes
- <C>. array
- <C>. Branch and loop statements
- Redis core article: the secret that can only be broken quickly
- Paddledtx v1.0 has been released, and its security and flexibility have been comprehensively improved!
- Pcl+vs2019+opencv environment configuration
- Causes and solutions of unreliable JS timer execution
- Web components series (11) -- realizing the reusability of mycard
- Huawei HMS core launched a new member conversion & retention prediction model
猜你喜欢
Yanjiehua, editor in chief of Business Review: how to view the management trend of business in the future?
How does zhiting home cloud and home assistant access homekit respectively? What is the difference between them?
Detailed explanation of unified monitoring function of multi cloud virtual machine
Cloud native 04: use envoy + open policy agent as the pre agent
Install and initialize MySQL (under Windows)
Several methods of obtaining function annotation text on pycharm
Teach you how to create and publish a packaged NPM component
New features of redis 6.0: take you 100% to master the multithreading model
Redis core article: the secret that can only be broken quickly
Share a billing system (website) I have developed
随机推荐
Barrier of cursor application scenario
Huawei in application review test requirements
R language quantile autoregressive QAR analysis pain index: time series of unemployment rate and inflation rate
The latest promo! 1 minute to understand the charm of the next generation data platform
DICOM to NII
Nnformer reading notes
Matlab_ two point zero five
Day 29/100 local SSH password free login to remote
Besides using hackbar, how can I make post requests
Huawei fast application access advertising service development guide
<C>. Calculation date to day conversion
Corporate finance formula_ P1_ Accounting statement and cash flow
How does jest expose specific problems in complex judgment conditions?
Measurement index SSMI
Redis thread level reentrant distributed lock (different unique IDs can be locked cyclically)
Exploration of advanced document editor design in online Era
Curtain down and departure
Splunk series: Splunk data import (II)
HMS core actively explores the function based on hardware ear return, helping to reduce the overall singing delay rate of the singing bar by 60%
About eruake learning