Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language keywords const and volatile

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to use the C language keywords const and volatile. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

1. Const read-only variable

The variable modified by const is read-only, essentially a variable

Const-modified local variables allocate space on the stack

Const-decorated global variables allocate space in the global data area

Const is useful only at compile time and useless at run time

The variable modified by const is not a real constant, it just tells the compiler that the variable cannot appear to the left of the assignment symbol.

II. Bifurcation of const global variables

In modern C language compilers, modifying const global variables will cause the program to crash.

Note: standard C language compilers do not store cons t-decorated global variables in read-only storage, but in modifiable global data areas, whose values can still be changed.

Let's look at a piece of code:

# include const int g_cc = 2; int main () {const int cc = 1; int* p = (int*) & cc; printf ("cc =% d\ n", cc); * p = 3; printf ("cc =% d\ n", cc); p = (int*) & gallecc; printf ("g_cc =% d\ n", g_cc); * p = 4 Printf ("g_cc =% d\ n", g_cc); return 0;}

The following is the output:

The above code shows that the local variable modified by const can modify the value through the pointer, but the global variable modified by const cannot modify the value through the pointer, and a segment error will occur.

III. The essence of const

Const in C language makes variables read-only.

Const in modern C compilers stores variables with global lifecycles in read-only storage (variables decorated by staic also have global lifecycles, so they are also stored in read-only storage after decorating with const)

Const cannot define a constant in the real sense

Let's take a look at a piece of code that analyzes the nature of const:

# include const int g_array [5] = {0}; void modify (int* p, int v) {* p = v;} int main () {int const i = 0; const static int j = 0; int const array [5] = {0}; modify ((int*) & I, 1); / / ok / / modify ((int*) & j, 2) / / error modify ((int*) & array [0], 3); / / ok / / modify ((int*) & g_array [0], 4); / / error printf ("I =% d\ n", I); printf ("j =% d\ n", j); printf ("array [0] =% d\ n", array [0]) Printf ("g_array [0] =% d\ n", g_array [0]); return 0;}

The following is the output:

If the comment is removed, a paragraph error will be reported:

This corresponds to the above, if you modify the variables of the global life cycle modified by const, the program will crash.

4. Const modifies function parameters and return values

The parameter of the const modified function indicates that you do not want to change the value of the parameter in the body of the function.

The return value of the const modifier function indicates that the return value is immutable, which is mostly used in the case of returning a pointer.

Tip: the literal amount of strings in C is stored in read-only storage, and const char* pointers are needed in the program.

Let's take a look at a piece of code that const decorates the function parameters and return values:

# include const char* f (const int i) {/ / I = 5; return "Autumn Ze";} int main () {const char* pc = f (0); printf ("% s\ n", pc); / / pc [6] ='_'; / / printf ("% s\ n", pc); return 0;}

The following is the output:

If you uncomment the following statement

/ / pc [6] ='_'; / / printf ("% s\ n", pc)

If you run the program, you will report an error, and you cannot try to modify the read-only variable:

V. volatile parsing

Volatile can be understood as "compiler warning pointer"

Volatile tells the compiler that it must fetch the value of a variable from memory every time

Volatile mainly modifies variables that may be accessed by multiple threads

Volatile can also modify variables that may be changed by unknown factors.

As follows:

These are all the contents of this article entitled "how to use the C language keywords const and volatile". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report