Functions of static, const, and volatile keywords

Source: Internet
Author: User

What is the role of the static keyword?
Few people can answer this simple question completely. In the C language, the keyword static has three obvious functions:
1. In the function body, a variable declared as static remains unchanged when the function is called.
2. In a module (but in the function body), a variable declared as static can be accessed by all functions in the module, but not by other functions outside the module. It is a local global variable.
3. In a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.
Most respondents can answer part 1 correctly, and some can answer part 2 correctly, but few can understand part 3. As a qualified software engineer, we need to understand the role of Part 3 and the benefits and importance of localized data and code scope.

Int teststatic ()
{
Int x = 1;
X ++;
Return X;
}
Main ()
{
Int I;
For (I = 0; I <5; I ++)
Printf ("% d/N", teststatic ());
}

Output:

2

2

2

2

2

 

Int teststatic ()
{
Static int x = 1;
X ++;
Return X;
}
Main ()
{
Int I;
For (I = 0; I <5; I ++)
Printf ("% d/N", teststatic ());
}

Output:

2

3

4

5

6

What is the purpose of const?

(1) definable
Const constant

(2) const can modify function parameters, return values, and even function definitions. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.
Const means "read-only ". It is very important to differentiate the functions of the following code.

 

Const int;
Int const;
Const int *;
Int * const;
Int const * a const;

 

  • The first two functions are the same. A is a constant integer.
  • Third, it means that a is a pointer to a constant INTEGER (that is, an integer cannot be modified, but a pointer can be ).
  • The fourth meaning is that A is a constant pointer to the integer number (that is, the integer number pointed to by the pointer can be modified, but the pointer cannot be modified ).
  • The last one means that A is a constant pointer to a constant INTEGER (that is, the pointer to an integer cannot be modified, and the pointer cannot be modified ).

(1) The keyword const is used to send useful information to people who read your code. For example, adding the const keyword before a function parameter means that this parameter is not modified in the function body and is a "input parameter ". When there are multiple parameters, the function caller can clearly identify which are input parameters and which are possible output parameters based on whether the const keyword exists before the parameter.

(2) the rational use of the keyword const can enable the compiler to naturally protect those parameters that do not want to be changed, so as to prevent them from being accidentally modified by code, so as to reduce the appearance of bugs.

Const contains more meanings in C ++, while in C, it only means: "common variables that can only be read ", it can be referred to as "unchangeable variables" (This statement seems to be an easy-to-use, but most accurately expresses the essence of const in C ), the constants required in the compilation phase can only be defined in the # define macro! Therefore, the following program in C language is invalid:

Const int size = 10;
Char A [size];/* invalid: variables cannot be used during compilation */

 

The following is an example of using const at work:

Const unsigned char * pbydata;

Unsigned char const daypermonth [12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };

Extern void dp_write_dsp_block (const word * bp, word Len );

Const unsigned short cnstuni2intermapsparseidx [];

 

 

Volatile problems:

The syntax of volatile is the same as that of const, but volatile means that "beyond the scope recognized by the compiler, this data can be changed ". Somehow, the environment is changing data (possibly through multi-task processing), so volatile tells the compiler not to make any assumptions about the data without authorization-this is especially important to start from optimization. If the compiler says, "I have read the data into the register and it is not in contact with the register ." In general, it does not need to read this data. However, if the data is modified by volatile, the compiler cannot make such assumptions. Because the data may be changed by other processes, the compiler must re-read the data rather than optimize the code. Just like creating a const object, a programmer can also create a volatile object, or even create a const object.
Volatile object. This object cannot be changed by programmers, but can be changed through external tools.

The volatile object must re-read the value of this variable every time it is accessed, instead of using the backup stored in the register. Examples of volatile variables are as follows:

. Hardware registers of parallel devices (such as status registers );

A non-automatic variable that will be accessed in the interrupt service subroutine );

. Variables shared by several tasks in multiple ready-to-use applications.

A const parameter can also be volatile, and a pointer can also be volatile. However, be careful when programming and ensure that it is not accidentally modified.

What is the role of the static keyword?
Few people can answer this simple question completely. In the C language, the keyword static has three obvious functions:
1. In the function body, a variable declared as static remains unchanged when the function is called.
2. In a module (but in the function body), a variable declared as static can be accessed by all functions in the module, but not by other functions outside the module. It is a local global variable.
3. In a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.
Most respondents can answer part 1 correctly, and some can answer part 2 correctly, but few can understand part 3. As a qualified software engineer, we need to understand the role of Part 3 and the benefits and importance of localized data and code scope.

Int teststatic ()
{
Int x = 1;
X ++;
Return X;
}
Main ()
{
Int I;
For (I = 0; I <5; I ++)
Printf ("% d/N", teststatic ());
}

Output:

2

2

2

2

2

 

Int teststatic ()
{
Static int x = 1;
X ++;
Return X;
}
Main ()
{
Int I;
For (I = 0; I <5; I ++)
Printf ("% d/N", teststatic ());
}

Output:

2

3

4

5

6

What is the purpose of const?

(1) definable
Const constant

(2) const can modify function parameters, return values, and even function definitions. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.
Const means "read-only ". It is very important to differentiate the functions of the following code.

 

Const int;
Int const;
Const int *;
Int * const;
Int const * a const;

 

  • The first two functions are the same. A is a constant integer.
  • Third, it means that a is a pointer to a constant INTEGER (that is, an integer cannot be modified, but a pointer can be ).
  • The fourth meaning is that A is a constant pointer to the integer number (that is, the integer number pointed to by the pointer can be modified, but the pointer cannot be modified ).
  • The last one means that A is a constant pointer to a constant INTEGER (that is, the pointer to an integer cannot be modified, and the pointer cannot be modified ).

(1) The keyword const is used to send useful information to people who read your code. For example, adding the const keyword before a function parameter means that this parameter is not modified in the function body and is a "input parameter ". When there are multiple parameters, the function caller can clearly identify which are input parameters and which are possible output parameters based on whether the const keyword exists before the parameter.

(2) the rational use of the keyword const can enable the compiler to naturally protect those parameters that do not want to be changed, so as to prevent them from being accidentally modified by code, so as to reduce the appearance of bugs.

Const contains more meanings in C ++, while in C, it only means: "common variables that can only be read ", it can be referred to as "unchangeable variables" (This statement seems to be an easy-to-use, but most accurately expresses the essence of const in C ), the constants required in the compilation phase can only be defined in the # define macro! Therefore, the following program in C language is invalid:

Const int size = 10;
Char A [size];/* invalid: variables cannot be used during compilation */

 

The following is an example of using const at work:

Const unsigned char * pbydata;

Unsigned char const daypermonth [12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };

Extern void dp_write_dsp_block (const word * bp, word Len );

Const unsigned short cnstuni2intermapsparseidx [];

 

 

Volatile problems:

The syntax of volatile is the same as that of const, but volatile means that "beyond the scope recognized by the compiler, this data can be changed ". Somehow, the environment is changing data (possibly through multi-task processing), so volatile tells the compiler not to make any assumptions about the data without authorization-this is especially important to start from optimization. If the compiler says, "I have read the data into the register and it is not in contact with the register ." In general, it does not need to read this data. However, if the data is modified by volatile, the compiler cannot make such assumptions. Because the data may be changed by other processes, the compiler must re-read the data rather than optimize the code. Just like creating a const object, a programmer can also create a volatile object, or even create a const object.
Volatile object. This object cannot be changed by programmers, but can be changed through external tools.

The volatile object must re-read the value of this variable every time it is accessed, instead of using the backup stored in the register. Examples of volatile variables are as follows:

. Hardware registers of parallel devices (such as status registers );

A non-automatic variable that will be accessed in the interrupt service subroutine );

. Variables shared by several tasks in multiple ready-to-use applications.

A const parameter can also be volatile, and a pointer can also be volatile. However, be careful when programming and ensure that it is not accidentally modified.

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.