static extern const volatile

static extern const volatile

There are many ways to define variables in C language, and the places of use are also different. Here is a summary of several types, which will be of great help to the actual use of programming or interviews.

1. static: static variable

a. When we declare a global variable as static: only its scope becomes the source file, that is, the attribute changes from external to internal, and the other remains unchanged;

b. When we declare the function as static: its scope becomes the source file, that is, the attribute changes from external to internal;

c. When we declare a local variable as static: the default initialization value is 0, and it is initialized only when it is first defined; the memory storage area is no longer a stack, but a static storage area; the life cycle is no longer the function , But the entire process; the other remains unchanged.

2.extern: reference variable

Reference variables generally need to be used often, because in C language, global variables and functions are default extern attributes. When other files want to use variables in another file, they need to use extern to declare.

3.const: read-only variable

Read-only variables are also called constants. Variables declared by const must be initialized when they are defined. as follows:

const int num = 10; 

Initialize at the definition, and the value of the variable is not allowed to change. Since the value of the variable is not allowed to change, then what is the use of this variable definition?

First of all, when we define an array, the size of the array can be represented by a constant defined by const. This is the same as #define, but it is type-safe. #Define is a preprocessing command, which simply performs character replacement. The compiler performs type checking on variables defined by const.

Secondly, when we need a variable that does not change, we can use const, for example, to define a person ’s gender. Since you were born, you have already determined your gender. No accidents, this life will not change. , So it is defined as read-only.

Of course, some people think that it is okay not to define it as const, as long as you do n’t change it, but if that is the case, you need to control it manually. What if you forget it one day, what if you change it? So for some read-only or constant is best to use const to define.

When we put const and pointer variables together, the problem becomes complicated. For example, we define as follows:

const int *p1;
int const *p2;
int * const p3;
int const * const p4;
  • The pointer variable p1: const before the data type modifies the object pointed to by p1, so the value of the object pointed to by p1 is a constant read-only and cannot be changed, but p1 itself can be changed;

  • Pointer variable p2: const before *, this situation is the same as p1;

  • Pointer variable p3: const after *, modify the variable p3, so the variable p3 itself is a constant read-only, and the object pointed to by p3 can be changed;

  • Pointer variable p4: There are two consts to modify the objects pointed to by variables p4 and p4, so p4 itself and the object pointed to by p4 are constant read-only and cannot be changed.

In fact, these are very easy to remember, just look const in *front or *behind, in *front modified object pointer is pointing in *the pointer itself is modified.

Let's take a simple example to illustrate:

int main(){
    int num1 = 0;
    int num2 = 1;
    int num3 = 2;
    int num4 = 3;
    const int *p1;
    int const *p2;
    //int * const p3; //error(1)
    int * const p3 = &num3;
    //int const * const p4; //error(2)
    int const * const p4 = &num4;
    p1 = &num1;
    //*p1 = 100;//error(3)
    num1 = 100;//此时*p1 = 1;
    //p3 = p4;//error(4)
    *p3 = 100;
    //p4 = p3;//error(5) 
  
    return 0;
}   

In the above code, error (1) and error (2) are easy to understand. Because const is *behind, the pointers p3 and p4 are read-only and must be initialized when they are defined.

error (3) is because the const is *before the pointer to p1 , so the object pointed to by p1 cannot be changed.

error (4) and error (5) are because there is const *after p3, p4 , so the pointer itself is read-only and cannot be changed after initialization.

4.volatile: volatile variable

volatile reminds the compiler that the variables defined behind it may change at any time, so every time the compiled program needs to store or read this variable, it will directly read the data from the variable address.

If there is no volatile keyword, the compiler may optimize reading and storage, and may temporarily use the value in the register. If this variable is updated by another program, there will be inconsistencies. Generally used in the following places:

a. Hardware registers of parallel devices (such as: status register)

b. Non-automatic variables that will be accessed in an interrupt service subroutine

c. Variables shared by several tasks in a multi-threaded application

The following is an example. In DSP development, we often need to wait for the trigger of an event, so we often write such programs:

short flag;
void test()
{
do1();
while(flag==0);
do2();
}

This program waits for the value of the memory variable flag to become 1 (doubt here is 0, a little doubt), and then runs do2 (). The value of the variable flag is changed by another program. This program may be a hardware interrupt service program.

For example: if a button is pressed, an interrupt will be generated to the DSP, and the flag is changed to 1 in the key interrupt program, so that the above program can continue to run. However, the compiler does not know that the value of flag will be modified by other programs, so when it is optimized, it may first read the value of flag into a register, and then wait for that register to become 1.

If such optimization is unfortunately carried out, the while loop becomes an infinite loop, because the contents of the register cannot be modified by the interrupt service routine. In order for the program to read the value of the real flag variable every time, it needs to be defined as the following form:
volatile short flag;

It should be noted that it may run normally without volatile, but it may not run normally after modifying the optimization level of the compiler. Therefore, there is often a problem that the debug version is normal, but the release version is not normal. For safety reasons, as long as you wait for other programs to modify a variable, add the volatile keyword.

5. Pointer variable

In the introduction of const, a lot of definitions about pointer constants are given, and here are some definitions about pointer variables.

a) int a; represents a memory space, this space is used to store an integer (int);

b) int * a; represents a memory space, this space is used to store a pointer, this pointer points to a space to store an integer, namely the space mentioned in a);

c) int ** a; represents a memory space, this space is used to store a pointer, this pointer points to a space where the pointer is stored, and the pointer in this space points to an integer. Simply put, it points to a space mentioned in b);

d) int ( *a) [10]; Represents a memory space, this space is used to store a pointer, this pointer points to an array of length 10 and type int; The difference from int ** a is that ++, + The result after = 1 is different, the other usages are basically the same. The above four types are shown in the figure above.

e) int (* a) (int); represents a memory space, this space is used to store a pointer, this pointer points to a function, this function has a parameter of type int, and the return type of the function is also int.

The analysis of static extern const volatile is here, and it will continue to be updated when you feel it.

Note: The above content is some of my experience accumulated in the learning process, it is inevitable that there will be some knowledge referring to other articles. If there is any infringement, please notify me in time, I will delete or mark the source of the content in time. Please point out, to explore and learn. The article only serves as a guide. For detailed data analysis, please see the C language related tutorials. Thank you for your review.

Published 106 original articles · praised 76 · 130,000 visits +

Guess you like

Origin blog.csdn.net/Creator_Ly/article/details/85877498