C language extern effect (global variable)

Source: Internet
Author: User

When writing programs in C, we often encounter a situation where we want to define a global variable in the header file and then include it in two different C files, which we hope will be shared among two files .

For example, there are three files main.c, COMMON.C, and Common.h under Project folder project, where Common.h files are included in the MAIN.C and common.c files, respectively. Now you want to declare a character variable key, which is common in main.c and COMMON.C. as shown in the following:

Some people think, since it is to want to use two files, it is in the common.h to declare a unsigned char key, and then because of the inclusion of the relationship, in MAIN.C and common.c are visible, so it can be shared.

This idea is actually a lot of beginners will think of, think of really reasonable, but actually write out, we found that compile time compiler prompt error, general hints are similar to: Error:L6200E:Symbol key multiply defined (by COMMON.O and MAIN.O). This means that the compiler thinks that we have repeatedly defined the variable key. This is because the # include command is the same as moving the contents of the head file to the # include location, so the equivalent of MAIN.C and COMMON.C executes a unsigned char key, and The global variables in the C language are visible within the project (or within the project), resulting in two variable keys in a project, which the compiler considers to be a duplicate definition.

  the correct workaround: use the extern keyword to declare the variable to be an external variable. Specifically, you define a global variable key in one of the C files, and then declare it once using the extern keyword in another C file that uses the variable key, stating that the variable is an external variable and is a global variable defined in the other C file. Please note my words here: definitions and declarations . For example, define the variable key in the Main.c file, declare the key variable in the Common.c file as an external variable, so that the two files can share the variable key, as shown in.

The code is as follows (write only the part that is relevant to the problem we are talking about):

(1) MAIN.C file

#include "Common.h"
unsigned char key;

  (2) common.c file:

#include "Common.h"
extern unsigned char key;

A lot of people see may be confused, here is a little bit, is actually the difference between the variable definition and the variable declaration , the variable definition uses "data type + variable name" form , the compiler needs to assign his memory unit instead, the variable declaration uses the form "extern variable type + variable name", which tells the compiler that this variable will be defined in the other external C file, and I'm just using it externally . The compiler does not allocate memory space to him, and then allocates the memory space when the variable definition is actually encountered.

Since many people learn C from the beginning, the definition variable is called the declaring variable , and it is called wrong from the beginning, so the difference between definition and declaration is now unclear. If you can't understand it, think about the definition and declaration of a function, a function definition is a function entity, the compiler compiles the function and allocates memory space, and the function declaration does not generate a function function entity, just tells the compiler that this is a function. This function will define the entity later, I just use it in advance, the compiler will continue to compile, if the sub-function is written in the main function, then the declaration is necessary, if you do not declare the function compiler does not know that this is a function, compile will error.

Reprinted from: http://dlutnaga.blog.sohu.com/176525248.html

C language extern effect (global variable)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.