Static and extern understanding

Source: Internet
Author: User

C language static modifier function detailed parsing (http://www.jb51.net/article/40520.htm)

In C, the literal meaning of static can easily lead us astray, in fact, it has three functions.
The first and most important piece of the article is: Hide.
When we compile multiple files at the same time, all global variables and functions that do not have a static prefix have global visibility. For the understanding of this sentence, I would say for example. We want to compile two source files at the same time, one is A.C and the other is main.c.

Here's what a.c.

Char ' A ' // global variable void msg () {    printf ("hello\n");}

Here's what main.c.

int Main (void) {       externchar A;    // extern variable must be declared before    use printf ("", a);    (void) msg ();     return 0 ;}

The running result of the program is:
A Hello

you might ask: Why are global variables A and function msg defined in A.C used in MAIN.C? As mentioned earlier, all global variables and functions that do not have a static prefix have global visibility and other source files can be accessed. In this example, a is a global variable, MSG is a function, and neither has a static prefix, so MAIN.C is visible for additional source files.

If static is added, it is hidden from other source files. For example, before the definition of a and MSG, add STATIC,MAIN.C to see them. With this feature, you can define a function with the same name and a variable of the same name in different files without worrying about naming conflicts. Static can be used as a prefix for functions and variables, and for functions, static is limited to hiding.

Detailed parsing of static and extern usage in C language (http://www.jb51.net/article/41686.htm)

One, static and extern:
We will encounter many source documents under big projects.

Document A.C

Static int // only in a document int J;    // used in engineering. Static void init ()/         // only in the a document {}void CallMe ()          // use {   staticint sum in the project  ;}

The global I variable and the init () function above can only be used in A.C documents, where the scope of the global variable sum is only in CallMe. The global limits of the variable J and function CallMe () are extended to the entire project document. So it can be called with the extern keyword in the B.C below. extern tells the compiler that this variable or function is already defined in other documents.

Document B.C

extern int J;     // called in the A document. extern void CallMe ();  // called in the A document. int Main () {  ...}

The other usage of extern is that when C and C + + are mixed programming, if C + + calls a function or variable defined by the C source document, add extern to tell the compiler to name the function in C:
Document A.cpp call A.C inside the variable i and function CallMe ()

extern " C "  // invoking variables from C documents in a C + + document {   int  j;    void CallMe ();} int Main () {   CallMe ();}

Second, the Static law:
A, if the global variables are only accessed in a single C document, then the variable can be modified to a static global variable to reduce the coupling between the modules;

B, if the global variable is accessed only by a single function, then the variable can be changed to the static local variable of the function, in order to reduce the coupling degree between the modules;

C, the design and use of access to dynamic global variables, static global variables, static local variables of the function, you need to consider the re-entry problem;

Static and extern understanding

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.