Discussion on the use of static and extern keywords in C and C++

  • 2020-04-02 00:48:04
  • OfStack

The static keyword in C
In C, static can be used to modify local variables, global variables, and functions. Static works differently in different situations.
(1) modify local variables
Typically, local variables are stored in the stack area, and the local variable's life cycle ends when the statement block is executed. But if you decorate it with static, the variable lives in a static data field until the end of the execution. However, it should be noted here that although the life cycle and storage space of a local variable have changed after it is decorated with static, its scope has not changed, and it is still a local variable whose scope is limited to the statement block.
After modifying a local variable with static, the variable initializes only on the first run, and only once.
Such as:

#include<stdio.h>
void fun()
{
staticint a=1;
a++;
printf("%dn",a);
}
int main(void)
{
fun();
fun();
return0;
}

The program execution result is: 2
                              3
    This indicates that when the fun() function is called for the second time, the value of a is 2, and the initialization assignment is not done, the self-increment operation is carried out directly, so the result is 3.
    The system automatically assigns a value of 0 to a static local variable if it is not initialized, and '\0' to a character array.
(2) modify global variables
    For a global variable, it can be accessed either in the source file or in another source file of the same project (simply declared with extern).
    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
But if I change int a=1 to static int a=1 in file1.c;
Then the variable a cannot be accessed in file2.c. The reason is that modifying a global variable with static changes its scope from being visible to the entire project to being visible to the source file.
(3) modification function
Using a static modifier is much the same as modifying a global variable, except that it changes the scope of the function.
2. 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 and not to any specific object of that class. If a variable in a class is static, it is owned by the class and all its objects. They all exist in a single copy of the storage space. It can be called through classes and objects. For static member functions, only static member functions and static member variables can be accessed, and non-static member functions or variables cannot be accessed.
Three. Extern keywords
In C, the modifier extern is used before the declaration of a variable or function to say, "this variable/function is defined elsewhere and is to be referenced here."
As you can see from the above example, if you want to call a variable a in file1 in file2, you simply declare extern to call a, which is what extern does. Note here that the location of the extern declaration is also related to its scope. If declared in the main function, it can only be called in the main function and not in other functions. To call functions and variables in other files, simply include the file with #include. Why use extern? Because using extern speeds up the compilation process, it saves time.
Extern has another function in C++ that indicates the calling specification for C or C++ functions. For example, if you call a C library function in C++, you need extern "C" in your C++ program to declare the function you want to reference. This is for linkers, telling them to use the C function specification when linking. The main reason is that C++ and C programs compiled in the object code after the naming rules are different, using this to solve the problem of name matching.

Related articles: