当前位置:网站首页>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
边栏推荐
- JS canvas drawing an arrow with two hearts
- Does redis transaction support acid?
- Redis practice: smart use of data types to achieve 100 million level data statistics
- Detailed explanation of unified monitoring function of multi cloud virtual machine
- Boomfilter learning
- Intra domain information collection for intranet penetration
- Global netizens Yuanxiao created a picture of appreciating the moon together to experience the creativity of Baidu Wenxin big model aigc
- Interview records
- [golang] leetcode intermediate - the kth largest element in the array &
- Leetcode theme [array] -31- next spread
猜你喜欢

CSDN sign in cash reward

CiteSpace download installation tutorial

Splunk series: Splunk installation and deployment (I)

Avoid material "minefields"! Play super high conversion rate
Cloud native 04: use envoy + open policy agent as the pre agent

How does pycharm create multiple console windows for debugging in different environments?
Web components series (11) -- realizing the reusability of mycard
Literals and type conversions of basic data types

Leetcode daily question - 27 Remove element (simple)

Pcl+vs2019 configuration and some source code test cases and demos
随机推荐
DICOM to NII
Ensure the decentralization and availability of Oracle network
<C>. String comparison
Curtain down and departure
COMP9024
<C>. Figure guessing game
手机开户股票安全吗,买股票在哪开户?
Corporate finance formula_ P1_ Accounting statement and cash flow
Leetcode daily [2022 - 02 - 18]
What is the core journal of Peking University? An article will help you understand it thoroughly
Cloud native 04: use envoy + open policy agent as the pre agent
[harmonyos] [arkui] how can Hongmeng ETS call pa
How do I delete an entity from symfony2
Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
Huawei fast application access advertising service development guide
How does pycharm create multiple console windows for debugging in different environments?
Measurement index SSMI
How to close gracefully after using jedis
Leetcode topic [array] -33- search rotation sort array
Share several Threat Intelligence platforms