How to Use the extern and static delimiters

Source: Internet
Author: User

The use of extern and static should belong to the basic knowledge of C language. However, in actual work, we often see the use of these two qualifiers in the Code. To sum up, there are roughly two types: first, for the variables or functions in the module, I don't know whether static or extern modifiers are added. I don't know how to use them correctly when I add a qualifier. Therefore, it is necessary to repeat the old words and describe them.

 

Simply put, remember two sentences,

 

1 static: Modified variables or functions cannot be used by any module other than this module; extern, on the contrary, agrees to be used by other modules other than this module;

2. When the variable or program is not modified by static or extern, the variable or function can be used by other modules.

 

In this case, it may be empty. Check the program.

 

Suppose there are two modules A and B, and Module B wants to use some variables and functions defined in module A. What should we do?

 

1. A does not make a statement. B understands the implementation details of a and uses it by himself.

 

/*----------------------------------------------

Moudle_a.c

This module contains the variable and Function

That user defined

----------------------------------------------*/

 

// Declaration of the variable and Function

 

Int index;

 

Void userfunction (void );

 

 

 

// Implementation of the Function

Void userfunction (void)

{

.....

// Use the index

Irecord = index;

....

 

}

 

 

 

/*----------------------------------------------

Moudle_ B .c

This module contains the variable and Function

That user defined

----------------------------------------------*/

Extern int index;

Extern void userfunction (void );

 

// Operate on the 'index' and 'userfunction ()'

...

/*----------------------------------------------

Moudle_ B .c

This module contains the variable and Function

That user defined

----------------------------------------------*/

Extern int index;

Extern void userfunction (void );

 

// Operate on the 'index' and 'userfunction ()'

...

 

 

Here, when index and userfunction are declared in module A, A does not use any qualifier, so the compiler understands that there is no restriction. Therefore, it is allowed when Module B wants to use it. However, this is not a good style. It is not recommended because it doesn't mean anything about itself.

 

2. A proactively discloses and B is in normal use;

 

/*----------------------------------------------

Moudle_a.h

This module contains the prototype information

----------------------------------------------*/

 

// Declaration of the variable and Function

 

Extern int index;

 

Extern void userfunction (void );

 

 

 

 

/*----------------------------------------------

Moudle_a.c

This module contains the variable and Function

That user defined

----------------------------------------------*/

# Include "moudle_a.c"

 

// Initialize the variable

...

 

// Implementation of the Function

Void userfunction (void)

{

.....

// Use the index

Irecord = index;

....

 

}

 

 

 

 

/*----------------------------------------------

Moudle_ B .c

This module contains the variable and Function

That user defined

----------------------------------------------*/

# Include "moudle_a.h"

 

// Operate on the 'index' and 'userfunction ()'

...

 

In this example, module A provides a header file that discloses information about index and userfunction and explicitly provides it to other modules. When Module B contains the header file of module A, you can use index and userfunction. Modules A and B cooperate with each other, and B is recommended for the use of variables and functions in.

 

3 A active protection, B cannot be used;

 

/*----------------------------------------------

Moudle_a.c

This module contains the variable and Function

That user defined

----------------------------------------------*/

 

// Declaration of the variable and Function

 

Static int index;

 

Static void userfunction (void );

 

// Initialize the variable

...

 

// Implementation of the Function

Void userfunction (void)

{

.....

// Use the index

Irecord = index;

....

 

}

 

 

/*----------------------------------------------

Moudle_ B .c

This module contains the variable and Function

That user defined

----------------------------------------------*/

# Include "moudle_a.h"

 

Extern int index;

 

Extern void userfunction (void );

 

 

// Operate on the 'index' and 'userfunction ()'

...

 

In this example, module A uses static to limit the scope of the index and userfunction functions. It clearly indicates that they do not want to be referenced by other modules. At this time, even if Module B understands the specific information of index and userfunction in module A, it tries to use the extern Declaration and will not succeed when referenced in its own code, causing compilation errors.

 
Now, it should be clear.

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.