Description of the C ++ keywords static, const, and volatile

Source: Internet
Author: User

1 static keyword

First, it indicates that the defined variables are static and the memory space is allocated in the static storage area. There are three main usage methods in C ++.

(1) use static keywords in Functions

Static int Var = 0;

Int var2 = 0;

Int main ()

{

VaR ++;

Var2 ++;

Printf ("% d", VAR, var2 );

Return 0;

}

At this point, the scope of VaR is the entire file. Here, var2 is also mentioned. Its scope is the entire project, that is, other files in this project can also access var2, but cannot access var, this is a difference between a global variable and a static variable.

(2) Use the static keyword inside the Function

Void test ()

{

Static int var;

Printf ("% d", VAR );
}

Int main ()

{

Test ();

Return 0;

}

The scope of VaR is limited to the test () function, but it is also allocated to the static storage areaProgramIt occupies the memory during the running process, but it is only valid in test.

2 volatile keywords

It indicates a variable, even if the programCodeIf the memory unit is not modified, its value may also change to eliminate Compiler optimization.

For example, if a variable value is used continuously in a program, the compiler will cache the variable and read it directly from the cache next time, instead of reading the memory, in this way, the read data may have been modified. When this keyword is added, the compiler will be notified not to perform such optimization.

 

3 const keywords

After a const is added to the global variable in C ++, the scope of the const is programmed by the entire project, which is slightly different from that in C ++.

(1) const char * const mouths [12] = {'Jan ', 'feb ',...........};

It is interpreted from the right to the left. This is a const array first, and each element of this array is a char * pointer. Finally, each pointer is of the const type, that is, the first const indicates that each pointer prevents the string from being modified. The second const indicates that each pointer must be the string originally pointed.

(2)Const int * PT: from right to left, a pointer points to a variable of the const int type, that is, the pointer points to a constant, but this pointer can also point to other constants, that is, Pt can be modified.

(3) int const * PT: This is a const-type pointer pointing to int-type variables, but * PT can be modified.

 

Related Article

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.