当前位置:网站首页>C language - growth diary-04- preliminary exploration of local variables (local variables)

C language - growth diary-04- preliminary exploration of local variables (local variables)

2022-06-11 07:43:00 Zhangxueheng

Today, when writing a topic of exchanging numbers , I came across such a problem

According to the definition of exchange function , All values should change , But the result is not so , In contrast, I have another program , I put the print function (printf) Put it in swap Function , Values change

It seems that the problem is printf Functions are located in different locations . Then I'll try to convert three groups of data printf Functions are also placed in swap Function instead of main Function .

  It seems that the result is the same as we predicted , Different positions lead to different results . I listened to the lecture again , It is found that the reason for this is the local variable ( Also called local variables ) Why .

For example, in a code block , Declared a variable

{

int a=0;
}

Put this in the main function , for example

Through this example, we can see the local variables a2 And global variables a1、a3 Change between .

also a2 Only affected by its block , because a3 be equal to a1, So local variables a2 Only in its code block .

I will continue to study c language knowledge , Share with you !

--------------------------------------------——--————————————————————————-

2022.2.28 Continue to update :

According to the rules of the local variables found :

 

 

 

That is to say, in the block {} The use of functions inside has limitations , for example :

  This is the definition of a local variable , It is only valid within the block , Find a block when you get out , It no longer exists

原网站

版权声明
本文为[Zhangxueheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020519312288.html