Static and extern keywords in C/C ++

Source: Internet
Author: User

StaticIt is a common modifier in C ++. It is used to control the storage and visibility of variables.Extern,"C" is a means to enable C ++ to call library files written by C. If you want to prompt the compiler to use C to process functions, use extern "C" to describe.

I. static keywords in C Language

In C, static can be used to modify local variables, global variables, and functions. In different cases, static has different functions.

(1) modify local variables

Generally, local variables are stored in the stack, and the lifecycle of local variables ends when the execution of the statement block ends. However, if static is used for modification, the variable is stored in the static data zone, and its lifecycle continues until the execution of the entire program ends. However, it should be noted that although the lifecycle and storage space of local variables have changed after modification with static, its scope has not changed, it is still a local variable and the scope is limited to this statement block.

After the local variable is modified with static, the variable is initialized only once at the first run.

For example:

 
 
  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 result: 2 3

Note that when you call the fun () function for the second time, the value of a is 2, and no initialization value is assigned. The auto-increment operation is directly performed, so the result is 3.

If static local variables are not initialized, the system automatically assigns 0 values to them for integer variables and '\ 0' for character arrays '.

(2) modify global variables

For a global variable, it can be accessed either in the source file or in other source files of the same project (you only need to declare it with extern ).

For example:

 
 
  1. // File1.c is available
  2. Int a = 1;
  3. File2.c
  4. # Include <stdio. h>
  5. Extern int;
  6. Int main (void)
  7. {
  8. Printf ("% d \", );
  9. Return 0;
  10. }

The execution result is 1.

However, if you change int a = 1 to static int a = 1 in file1.c;

In file2.c, variable a cannot be accessed. The reason is that modification of global variables with static changes the scope of its scope, from the original project visible to the current source file visible.

(3) Modifier

If you use static to modify a function, the situation is similar to that of modifying global variables, that is, changing the function scope.

Ii. static in C ++

Static in C ++ also has other functions. If a function in the class is modified using static in C ++, it indicates that the function belongs to a class rather than any specific objects of this class. If static modification is performed on a variable in the class, it indicates that the variable is owned by the class and all its objects. They only have one copy in the bucket. It can be called through classes and objects.

3. extern keyword

In C, the modifier extern is used before the declaration of a variable or function to indicate that "this variable/function is defined elsewhere and must be referenced here ".
In the above example, we can see that if you want to call the variable a in file1 in file2, you only need to declare it with extern to call a, which is the role of extern. Note that the location of the extern Declaration has a relationship with its scope. If it is declared in the main function, it can only be called in the main function, but not in other functions. In fact, to call functions and variables in other files, you only need to include the file with # include. Why use extern? Because the use of extern will accelerate the compilation process of the program, which can save time.

In C ++, extern has another function, which is used to indicate the call specifications of C or C ++ functions. For example, to call a C-Database Function in C ++, you must declare the function to be referenced using extern "C" in C ++. This is for the linker and tells the linker to use the C Function Specification for link. The main reason is that the naming rules in the target code are different after the C ++ and C Programs are compiled. This is used to solve the name matching problem.

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.