On static and extern keyword __c++ in C + +

Source: Internet
Author: User
Tags modifier

Static is a commonly used modifier in C + +, which is used to control the storage and visibility of variables. extern, "C" is a means to enable C + + to invoke C writing library files, if you want to prompt the compiler to use C to handle functions, then use extern "C" to explain.

The static keyword in a. C language

In the C language, static can be used to modify local variables, global variables, and functions. Static functions vary in different situations.

(1) Modifying local variables

In general, the local variable is stored in the stack, and the life cycle of the local variable ends at the end of the statement block execution. However, if you decorate with static, the variable is stored in the static data area, and its lifecycle lasts until the end of the execution of the program. However, it should be noted here that, although the local variable was decorated with static, its lifecycle and storage space changed, but its scope has not changed, it is still a local variable, the scope is limited to the statement block.

When a local variable is decorated with static, the variable is initialized only at the first run, and only once.

Such as:

#include <stdio.h> void Fun () {static int a=1; a++;   printf ("%d\n", a);    int main (void) {fun ();    Fun ();   return 0; }

Program execution results are: 2 3

Note that when the fun () function is invoked the second time, the value of a is 2 and no initialization assignment is performed, and the result is 3.

For static local variables, if not initialized, the reshaping variable system is automatically assigned a value of 0, and the character array is automatically assigned to '.

(2) Modifying global variables

For a global variable, it can be accessed either in the source file or in other sources of the same project (just use extern to declare it).

Such as:

There are file1.c int a=1;   file2.c #include <stdio.h> extern int A;   int main (void) {printf ("%d\", a);   return 0; }

The execution result is 1

However, if the int a=1 is changed to a static int a=1 in the file1.c;

Then the variable a is inaccessible to file2.c. The reason is that using static to modify a global variable changes its scope, from the original project visible to the source file visible.

(3) Modifier function

With the static modifier function, the situation is the same as modifying the global variable, that is, changing the scope of the function.

Two. Static in C + +

Static also has other functions in C + +, if a function in a class is decorated with static in C + +, it means that the function belongs to a class rather than to any particular object belonging to that class, and if a variable in the class is static decorated, the variable is a class with all its objects. They only have one copy in the storage space. Can be invoked through classes and objects.

Three. extern key word

In C, the modifier extern is used before the declaration of a variable or function to indicate that "this variable/function is defined elsewhere, to be referenced here".
As you can see in the above example, if you want to call variable a in file1 in File2, you can invoke a with extern, which is the role of extern. Notice here that the location of the extern declaration is also related to its scope, and if it is declared in the main function, it can only be invoked in the main function and cannot be invoked in other functions. In fact, to invoke functions and variables in other files, simply include the file in the #include, why use extern. Because extern accelerates the program's compilation process, this saves time.

extern has another role in C + + that indicates the call specification for C or C + + functions. For example, calling C library functions in C + + requires the use of extern "C" in the C + + program to declare the function to be referenced. This is for the linker, tell the linker to link in the C function specification. The main reason is that C + + and C program after the compilation is completed in the target code different naming rules, use this to solve the problem of name matching.

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.