“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.
The variable type qualifiers const and volatile are often used in C, but const and volatile can be used together. Here is an analysis of the use of these three cases.
1, the const
A constant is a constant that cannot be assigned, incremented or decremented to change its value.
For ordinary variables, it is simple to use. Let’s say you define a variable
Const int I = 10;
You can only use the value of I in your code, you can’t change the value of I.
For Pointers, it’s a little more complicated to use.
const int *p;
This means that the value to which the pointer p points cannot be changed, but the value of the pointer itself can be changed, for example by pointing to another place.
Int * const p;
This means that the pointer itself cannot be modified, that is, p cannot point to any other address, but the value to which the pointer p points can be changed.
const int * const p;
This means that neither the pointer itself nor the value to which the pointer points can be changed.
There is a simple way to determine this: As long as const is to the left of the *, the value to which the pointer points cannot be changed. As long as const is to the right of the *, it means that the pointer itself cannot be changed. , that is, Pointers must point to the same address.
Here’s a simple example of using const. Set the integer variable I to const, and then print the value of I.The value of I can be printed normally. So let’s add 1 to the value of I.The compiler reports an error indicating that the value of a variable of read-only type I has been incremented. However, a variable that is const cannot always be read only. Sometimes a const qualified value can also be modified. More on that later.
2, volatile
Unstable unstable unstable unstable unstable unstable The use of this qualifier is mainly for the compiler. Tell the compiler that this value is easy to change and cannot be optimized while optimizing the code.
For example, there is the following situation:
val1 = x; val2 = x;
The compiler sees two variables that use x at compile time, but the value of x does not change. When val2 needs to use x, the compiler reads the value of x directly from the register, rather than from the original location where x was stored. If this variable x contains the value of an external sensor, then using the above statement, if optimized by the compiler, the value of the sensor is read unchanged. To solve this problem, you need to prefix the variable with volatile.
volatile val1 = x; volatile val2 = x;
So when the compiler compiles, it sees that val1 and val2 are volatile, and when val1 and val2 read x, it reads x from where it was originally stored. So when the value of x is the value of the external pull-up sensor, each read is a truly valid value.
3, const volatile
The const modifier is a constant, and the volatile modifier is volatile. Are the two used together to mean a volatile constant? Can a const change its value after it is modified? The answer is yes. Const objects cannot be changed within their scope, but can be changed outside of scope. How do you understand this?
Again, use the example above
const val1 = x;
The data of the sensor stored in X cannot be changed by code in the program, but the value of VAL1 will also change after the value of the sensor is changed. This is a bit abstract, so let’s use the most common example, such as the time to read the system now.We use a const rawTime object that stores the current system time. The system time can change at any time, but can still be modified by const, and the rawtime value also changes when the system time changes. So what does const qualify for? For example, assigning rawTime manually in a program.The compiler prompts rawtime to be const and cannot change its value. Const rawtime cannot be modified within the scope of the current main function, but outside of this function, the rawtime value can be modified by the system at runtime.
The volatile modifier indicates that the object cannot be modified in scope, but is mutable outside of scope and must be read from its original location each time it is read. Volatile and const are used together regardless of where they are; they are the same whether they are first or last.
For example, the system time is still read. After reading the time for the first time, the system time is read again after a delay of 3s. As can be seen from the printed results, the system time also differs by 3s. Using volatile and const to modify rawtime tells the compiler that the object cannot be modified in its scope, but is mutable outside of it. When it does, it reads directly from the original address without compiler optimization.
The three examples above should give you an idea of where the const, volatile, and const volatile qualifiers are used and what they mean.