Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the definition of local variables in a function body

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the problem of local variable definition in function body". In daily operation, I believe many people have doubts on how to understand the problem of local variable definition in function body. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubt of "how to understand the problem of local variable definition in function body"! Next, please follow the small series to learn together!

1. Definition of variables in functions

Let's look at the positions where variables a, b, c, and d are defined in the code:

void Test(void) { char a; //a position defined printf("a = %d\n", a); char b; //b position defined printf("b = %d\n", b); for(char c = 0; c

< 10; c++) //c定义的位置 { char d; printf("c = %d\n", c); printf("d = %d\n", d); } } 以上变量定义位置,相信很多人都看到过,有些朋友在实际编程中也常这么定义在这些位置。 上面这段代码C编译器编译会错吗?答案:按照C89标准编译就会出错;按照C99标准编译就不会出错。 2. 变量定义位置的区别 相信很多朋友在大学的时候都学过C和C++,学过C++的朋友都知道,上面那段代码变量定义的位置是很合理的。 但不知道大家了解过C89和C99的区别,区别的内容还是有很多,针对本文说下定义变量位置的区别。 C89标准是不允许在函数中随便位置声明变量,C语言中的变量声明不能放在任何可执行语句之后,只允许在函数(可执行语句)的开始处。如上一段代码,需要在【printf("a = %d\n", a);】这条语句之前。 C99及C++放松了限制,允许在函数体内任意位置声明变量。C99新的标准满足了在函数体内定义变量立马就使用的功能。但是,也要注意变量的作用域。如下面代码就会出错: void Test(void) { for(char c = 0; c < 10; c++) { printf("c = %d\n", c); } printf("c = %d\n", c); //这里有问题 } 3. MDK-ARM和EWARM中C99的配置 上面说的问题,就是出在IDE的配置上。EWARM工程默认是支持C99标准的,而MDK-ARM工程默认是不支持C99标准的。所以说,将一个支持C99的标准的代码拷贝到不支持C99标准的环境中,编译就会因为代码而出错。 MDK-ARM和EWARM只需要修改一下配置就可以支持C99标准。 (1) MDK-ARM的配置Project ->

Options for Target -> C/C++ Check "C99 Mode".

(2)EWARM configuration Project -> Options -> C/C++ Compiler -> Language 1 Select "C99" on it.

There is also a lot of controversy about the location of variable definitions: some people say that in order to be compatible with the C89 standard, they are uniformly written in front of executable statements; some people like the C99 standard later.

At this point, the study of "how to understand the definition of local variables in function bodies" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report