当前位置:网站首页>[C language] static modifies local variables
[C language] static modifies local variables
2022-06-26 16:58:00 【Green abundance is not green】
int function(int n)
{
int i = 1; // Local variables are not decorated
if (n >= 5)
return n;
n += i;
i++;
return function(n);
}
int main()
{
printf("%d\n",function(1));
return 0;
} here function Function , Shaping variables i Is not decorated , So in the function function Recursion , Function i Is redefined and assigned as 1( That's it function after , The original i Be destroyed , Create new i ), So the final function The return value of is 5
int function(int n)
{
static int i = 1; // Static modification , Give Way i There's something wrong with it function Still exist after
if (n >= 5)
return n;
n += i;
i++;
return function(n);
}
int main()
{
printf("%d\n",function(1));
return 0;
} This one below function Function , Variable i By static modification , So when a function recurses , Functional i Will not be destroyed ,i Will continue to increase , So finally function The return value of is 7 
So we can come to the conclusion that
static Modify local variables Changed the life cycle of the variable Let static local variables out of scope and still exist , By the end of the program , The life cycle is over .
边栏推荐
- Summary of all knowledge points of C language
- Overall context of concurrent programming
- Programmer interview guide - self introduction
- Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
- Screenshot of the answers to C language exercises
- 5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?
- Introduction to minimal API
- Science | 红树林中发现的巨型细菌挑战传统无核膜观念
- [matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion
- [Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
猜你喜欢
![[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity

建立自己的网站(16)

When a programmer is disturbed 10 times a day, the consequences are amazing!

Don't believe it, 98% of programmers are like this

Knowing these commands allows you to master shell's own tools

Teach you to learn dapr - 8 binding

NFT 交易市场社区所有化势不可挡

探讨:下一代稳定币

SQL injection for Web Security (3)

构造函数和析构函数
随机推荐
SQL injection for Web Security (3)
LeetCode Algorithm 24. 两两交换链表中的节点
Sandboxed container: container or virtual machine
Teach you to learn dapr - 8 binding
What is flush software? Is it safe to open an account online?
Pybullet robot simulation environment construction 5 Robot pose visualization
Scala Foundation (2): variables et types de données
Count the number of words in a line of string and take it as the return value of the function
Vibrating liquid quantity detecting device
内存分区模型
Calculate a=1, a2=1/1=a1
108. simple chat room 11: realize client group chat
Record the use process of fenics
Redis overview
Introduction to minimal API
What is the difference between digital collections and NFT
去中心化NFT交易协议将击败OpenSea
JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications
进军AR领域,这一次罗永浩能成吗?
Teach you to learn dapr - 6 Publish subscription