Talking about static and extern key-words in C + +

Source: Internet
Author: User

Original: http://developer.51cto.com/art/201104/256820.htm

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 use C to the compiler prompt to handle the function, then use extern "C" to illustrate.

I. Static keywords in the C language

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

(1) Modifying local variables

In general, the local variable is stored on the stack, and the life cycle of the local variable ends at the end of the statement block execution. However, if modified with static, the variable is stored in the static data area, and its life cycle continues until the end of the execution of the program. However, it is important to note that although the modification of a local variable with static, its life cycle and storage space have changed, but its scope has not changed, it is still a local variable, scope is limited to the statement block.

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

Such as:

    1. #include <stdio.h>
    2. void Fun ()
    3. {
    4. static int a=1; a++;
    5. printf ("%d\n", a);
    6. }
    7. int main (void)
    8. {
    9. Fun ();
    10. Fun ();
    11. return 0;
    12. }

Program execution results are: 2 3

The second time the fun () function is called, the value of a is 2, and no initialization assignment is made, and the self-increment operation is performed directly, so the result is 3.

For a static local variable, if it is not initialized, the system will automatically assign a value of 0 to the shaping variable, and for the character array, it will automatically be assigned a value of ' \ s '.

(2) Modifying global variables

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

Such as:

    1. Have file1.c
    2. int a=1;
    3. File2.c
    4. #include <stdio.h>
    5. extern int A;
    6. int main (void)
    7. {
    8. printf ("%d\", a);
    9. return 0;
    10. }

The execution result is 1

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

Then the file2.c is inaccessible to the variable A. The reason is that the modification of the global variable with static changes the scope of its scope, from the original project visible to the source file is visible.

(3) Modifier function

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

Two. Static in C + +

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

Three. extern keyword

In the C language, the modifier extern is used before the declaration of a variable or function to illustrate that "this variable/function is defined elsewhere and is referenced here."
As can be seen in the above example, if you want to call the variable A in file1 in File2, you only need to declare with extern to invoke a, which is the role of extern. It is important to note 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 called in the main function and cannot be called in other functions. In fact, to invoke the functions and variables in other files, simply include the file with # # included in it, why use extern? This saves time by using extern to speed up the process of compiling the program.

In C + +, extern has another role to indicate the invocation specification of C or C + + functions. For example, invoking C library functions in C + + requires that the function to be referenced is declared in the C + + program with extern "C". This is for the linker, telling the linker to link with the C function specification when linking. The main reason is that C + + and C programs are compiled in the target code after the naming rules are different, use this to solve the problem of name matching.

Talking about static and extern key-words in C + +

Related Article

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.