C ++ role of auto, static, register, extern of

C ++ role of auto, static, register, extern of

 auto, static, register, extern these four key variables are used to store instructions, and its role in the format:

        Storage variable type specifier variable name = variable initializer


Before you begin discussing the role of the four specifiers, it is necessary to explain the local variables ( Local the Variables ) and global variables (
, Ltd. Free Join the Variables
defined)

 Procedure is composed of several modules, interfere with each other between the modules work independently, the general definition of local variables within a function (including the main function), the age of the variables also will function completes destruction, global variables are used for the entire program, when the program That there is a compile-time, wait until the end of the program completely destroyed.

 

1  // global variables and local variables 
2 #include <the iostream>
 . 3  the using  namespace STD;
 . 4  int B = 100 ;
 . 5  int FUNC () {
 . 6      int A = 10 ;             // only acting on the inner function, the function call is completed automatically destroyed 
. 7      return a;
 . 8  }
 . 9  int main () {
 10      COUT << b << endl;         // output value of b is 100 
. 11      int a = 20 is ;
 12 is      COUT a << << endl;         // output value a 20 
13     
14     return 0;
15 }

 

 auto specifier

Auto , Chinese interpretation of " automatic ", in which the variable effect is out of scope (e.g., internal functions) automatically destroyed, under normal circumstances, Auto specifier can be omitted, because the computer program during execution has been achieved this function, for example:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  int main () {
 . 4      Auto int a = 2 ;         // away from the main function, a variable is automatically destroyed 
. 5      COUT a << << endl;
 . 6      
. 7      return  0 ;
 . 8 }

 

 static specifier

static , Chinese interpretation is " static ", whose role is defined only accept a variable initialization, can not accept a second time, figuratively speaking, my name called the Little Ming, I will not allow others to give me from the other name, (played does not recognize that). Examples are as follows:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  void FUNC () {
 . 4      static  int A = 10 ;
 . 5      A ++ ;
 . 6      COUT A << << endl;
 . 7  }
 . 8  int main () {
 . 9      FUNC ();     / / initialize a is 10, the output of a 11 
10      FUNC ();     // do not accept initialization, a 11, a output ++, equal to 12 is 
11      return  0 ;
 12 is }

 register specifier

Register , Chinese interpretation is "register", ie, the register type register distance CPU within the computer nearest a container, the variable is defined in this register inside, enables the extracted variables faster, corresponding effect program efficiency, so that the program efficiency is greatly improved, but the disadvantage is that a very small space register, they must be carefully considered when using the register type. Examples are as follows:

 

  1 register int number = 0; 

 extern specifier

Interpretation is "external variables", it belongs to the variable declaration, extern int int n n and the difference is that the former tells the computer, there is a variable of type int n defined elsewhere, please call if there are other files to find the definition. Examples are as follows:

 1 #include <iostream>
 2 using namespace std;
 3 void func(){
 4     extern int a = 10;
 5     cout<<a<<endl;
 6 }
 7 int a;
 8 int main(){
 9     
10     func();
11     return 0;
12 }

 

Guess you like

Origin www.cnblogs.com/whtmomo/p/11299413.html