C Language Learning notes--const and Volatile keywords

Source: Internet
Author: User
Tags volatile

1.const keywords

(1) The const modifier variable is read-only, it is not a true constant, an intrinsic or a variable, just tells the compiler not to appear on the left side of the assignment number!

(2) A const modified local variable allocates space on the stack

(3) The const-modified global variable allocates space in the Global data area (VC, GCC will put it in the constant area, bcc into the global data area)

(4) const only useful at compile time, useless at run time

#include <stdio.h>Const intG_CC =2;//bcc is stored in the global data area and can be modified. //VC, GCC store it in a constant area, cannot be modifiedintMain () {Const intCC =1;//stored in the stack    int* p = (int*) &cc; printf ("cc =%d\n", CC); *p =3; printf ("cc =%d\n", CC); P= (int*) &g_cc;printf ("g_cc =%d\n", G_CC); *p =4;//There is no problem under the BCC compiler, VC\GCC error occurs. printf"g_cc =%d\n", G_CC); return 0;}

The const in the C language makes the variable have a read-only property, but it is valid only during compilation and is not valid during run time, so you can use pointers to indirectly modify its value, one thing to note is that the const in modern C compilers (such as Vc, GCC) will have a global life cycle (such as global variables , the variable of the static property is stored in the read-only store, which causes the program to crash when the variable is modified.

#include <stdio.h>Const intg_array[5] = {0};voidModifyint* p,intv) {    *p =v;}intMain () {int Consti =0;//equivalent to const int i=0;    Const Static intj =0; int Constarray[5] = {0};//equivalent to const int array[5]={0};Modify ((int*) &i,1); Modify ((int*) &j,2);//ErrorModify ((int*) &array[0],3); Modify ((int*) &g_array[0],4);//Errorprintf"i =%d\n", i); printf ("j =%d\n", J); printf ("array[0] =%d\n", array[0]);p rintf ("g_array[0] =%d\n", g_array[0]); return 0;}

The Const keyword can be used to modify function parameters and return values

(1) The const modifier function parameter indicates that the value of the parameter is not expected to change in the function body

(2) The return value of the const modifier function indicates that the return value is immutable and is used in cases where the pointer is returned.

#include <stdio.h>int  main () {    constchar"I like" programming! " // stored in read-only memory}

const modifier function parameter and return value

#include <stdio.h>Const Char* F (Const inti) {i=5;//error, I cannot be a left value    return "I like programming!";}intMain () {Char* PC = f (0);//warning, the return value of F is const char*printf"%s\n", PC); pc[1] ='_';//error attempting to modify data in a read-only storeprintf"%s\n", PC); return 0;}
2.volatile keywords

(1) volatile can be interpreted as "compiler warning indicator word"

(2) volatile tells the compiler to take variable values each time it goes to memory

(3) Volatile major modifier variables that may be accessed by multiple threads

(4) Volatile can also modify variables that may be changed by unknown factors

Volatile force compiler reduces optimizations and must be evaluated every time in memory

int Ten int 0 ; int 0  = obj;sleep (= obj;

The compiler found that obj was not used as an lvalue at compile time, so it would be "smart" to replace obj directly with 10, and both A and B would be assigned a value of 10.

C Language Learning notes--const and Volatile keywords

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.