当前位置:网站首页>[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
- The function keeps the value of variable H to two decimal places and rounds the third digit
- Codeforces Round #802 (Div. 2)
- 防火 疏散 自救…这场安全生产暨消防培训干货满满!
- [suggested collection] 11 online communities suitable for programmers
- Structure the graduation project of actual combat camp
- 无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...
- Constructors and Destructors
- Redis migration (recommended operation process)
- Don't believe it, 98% of programmers are like this
猜你喜欢

Wechat app mall, review products, upload commodity pictures, and score Commodity Services

Stm32h7b0 replaces the h750 program, causing the MCU to hang up and unable to burn the program

Web3 decentralized storage ecological landscape

Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)

Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed

Basic requirements: 7 problems in singleton mode
![[suggested collection] 11 online communities suitable for programmers](/img/6b/d5c68e93384fd314d0cb27d9df1cb9.jpg)
[suggested collection] 11 online communities suitable for programmers

探讨:下一代稳定币

Redis' 43 serial cannons, try how many you can carry

20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
随机推荐
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
防火 疏散 自救…这场安全生产暨消防培训干货满满!
NFT 交易市场社区所有化势不可挡
SQL injection for Web Security (3)
Codeforces Round #802 (Div. 2)
Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?
Calculate the average of N numbers in the index group of X, and return the number that is less than the average and closest to the average through formal parameters
内存分区模型
Day10 daily 3 questions (3): String Matching in array
[从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
JS tutorial - printing stickers / labels using the electronjs desktop application
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
Greenplum数据库故障分析——semop(id=2000421076,num=11) failed: invalid argument
Teach you to learn dapr - 4 Service invocation
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!
Teach you to learn dapr - 3 Run the first with dapr Net program
Discussion: the next generation of stable coins