Usage analysis of Keyword const, static and volatile in embedded development

Source: Internet
Author: User
Tags modifier volatile

"1" Static:

1 Limit the scope of a variable or function

2 Set the storage domain of the variable

Common to have:

(1) static global variable (2) static local variable (3) static function

(1) Static global variable: 1,static global variable is only first made once, to prevent in other file cells are referenced , 2, only in the source file to define the variable, it is not available in other source files of the same source program.

(2) Static local variable: 1,static local variable is only initialized once, the next time based on the previous result value, 2, limited its scope of use

(3) Static function: 1, thestatic function is different from the normal function scope, only in this file. Functions that are used only in the current source file should be described as internal functions (static), and internal functions should be described and defined in the current source file.


2, the static function in memory only a copy of the normal function in each call to maintain a copy

The static usage scenario consists of three types:
Variables in the body of the modifier function
Variables outside the body of the modifier function
Modifier function
In the first case, static extends the life cycle of the local variable, and static local variables are not destroyed as the function ends, and the static local variable retains the value at the end of the last execution when its function is executed again. such as:

  1 #include <stdio.h>
  2 
  3 void Test ()
  4 {
  5     static int j = 1;
  6     printf ("%d\n", j);
  7     j + +;
  8}
  9 int main ()
 {one     test ();     test (); 
 0 return     ;
 15}
The result of the output is:
1
2
For the next two cases, static is scoped to the object it modifies, the static-decorated function, and the variables outside the function, are accessible only in the current code file, and other files are not directly accessible. When objects with duplicate names appear in multiple modules, they can be decorated with static.

"2"extern:

extern can be placed before a variable or function to represent the definition of a variable or function in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function. In addition, extern can also be used for link designation.

"3" volatile:

Definition: A variable defined as volatile means that the variable may be unexpectedly altered so that the compiler does not assume the value of the variable .

Precisely, the optimizer must carefully reread the value of the variable every time it uses it, instead of using a backup stored in the register.

Here are a few examples of volatile variables:

1. Hardware registers for parallel devices (e.g., status registers)

2). Non-automatic variable (non-automatic variables) that is accessed in an interrupt service subroutine

3. Variables shared by several tasks in multi-threaded applications

"4" Const:

(1) Can protect the modified things to prevent accidental modification, enhance the robustness of the program.

(2) The compiler usually does not allocate storage space for ordinary const constants, but instead stores them in a symbol table, which makes it a constant during compilation, with no operation to store and read memory, making it highly efficient

(3) A const-defined constant that has only one copy during program operation

As a programmer, when we see the keyword const, the first thought should be: Read-only. Because it requires that the object being decorated is constant and cannot be modified and two assignment operations (cannot appear as a left value). Look at a few examples:

     const int A;
     The int const a;//is equivalent to the preceding line of code, representing a number of constant reshaping.
     int *const A;//const has "left-bound" sex, which is the const modifier *, then, it is not difficult to understand that the sentence represents a constant pointer to an integer, a point to the integer can be modified, but pointer a can not be modified. The
     const int *a;//is equivalent to the following line, which, according to the "left-bound" sex, is modified by a const (*A), or an integer, so that the two sentences indicate that the pointer points to a constant integer.
     int const *a;
     int const *A CONST;//According to the "left binding" property, the first const modifier (*), the second const modifier (a), therefore, this sentence represents a constant pointer to a constant integer.
The proper use of the Const keyword not only enables the compiler to protect the corresponding data well, but also intuitively conveys useful information to the reader of the code.

Other:

extern:

Http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777431.html

Const:

Http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777431.html

the difference between static and extern

Http://www.chuxue123.com/forum.php?mod=viewthread&tid=548&extra=page%3D1

volatile: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html

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.