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

What is the easy to overlook usage in extern

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you what the easy-to-ignore usage of extern is. It is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The meaning of extern

First of all, when we see the emergence of extern, we should understand that it means the overall situation. Second, when we see extern modifying a variable, we should be able to distinguish between a declaration of a variable and, accordingly, a definition of a variable. With regard to the difference between the declaration of variables and the definition of variables, the author has seen a sentence that is described in this way. The existence of variables requires a storage space, which is like a house. The declaration of variables is like you have got the property certificate for the house, but you have not yet got the house. The definition of the variable is that you have already lived in the house and are already using the house. With regard to the global variables declared by extern, there are two most common types:

Variables that are not declared with extern are defined directly in the header.

Declare variables with extern, and then define variables in a .c file

The variables defined in the above two ways are called global variables, but the scope of the variables is different, the scope of the first is limited to this file, and the scope of the global variables defined in the second way is the whole project. Another method is that the author found a usage when reading the RT_Thread source code, and its scope is between the above two.

Define directly in the header

As shown in the figure, the iNum variable is defined in the header of the test1.c file, and both function0 () and function1 () in test1.c can use this variable. Finally, the two functions are called in the main function, and the output value is 2. But iNum cannot be used in main.c files, that is, it cannot be used across files.

Extern declares variables

In order to enable variables to be used across files, that is, the scope is extended to the entire project, the use of extern declaration variables is introduced here. For the author's habit, I usually put the variables declared by extern in the .h file, as shown in the following figure:

In this way, iNum can be used across files, but for cross-file use, the header file is included, including the header file where the extern declaration variable is located.

Declare variables within a function with extern

Using extern to declare variables within the function is different from the scope of the first two, between the above two, the specific usage is as follows: I defined a variable iNum in the header of the test.c file, but did not declare it in the header file, so the scope of this variable as a global variable is the test.c file, but unlike the first example, I did not declare it in the header file But I declare iNum in the function3 () function in the function.c file, so its scope is extended to the function3 () function. When the function3 () function ends, the scope of iNum narrows down to the test.c file.

The above is what the usage of extern is easy to overlook. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report