Understanding of the declarations and definitions

  You can take a look at this blog .

  A variable or function can be declared many times , but most can only be defined once , and in the variable or function when used , must be defined once . (If the function / variable is not used, it can not be defined only declaration)

  First, determine whether a statement declaring variables : If there is before the variable extern keyword , it is a statement , if the former variable is not extern keyword , then it is defined :

extern  int i;     // declare i 
int i;             // definition of i, for i is initialized but 
int i = . 5 ;         // is a definition, while i initialized.

 

  In short: to determine whether a statement is a declaration statement, the key is to look at the statement contains extern keyword , rather than look at the statement contains "=", "=" role is to initialize variables .

  One exception:  extern int i = 5 ;  this statement is a definition of the role of this sentence it is to define a type of int global variable called i, i is the initial value of 5.

  You can not use the statement in the function body, will get an error: error C2205: 'i': CAN not Block the initialize extern with the Variables scope. It can only be used outside the function, and the role of "int i = 5;" the same. So " extern int i = 5 " little value this statement.

  

  Second, determine whether the statement is a function declaration : If the function contains an implementation of the body ( i.e., " {} " ) , it is a function defined otherwise function declaration .

void Fun ()         // function definition 
{
}

void Fun ()     // function declaration

  Declare a function or variable is actually said to the compiler: This function / variable is present . Only use when the variable / function, the compiler will go to confirm the variable / function is truly exist, if it is found that variable / function does not actually exist, it will build failure.

  Variable / function is truly exist depending on whether the definition of the variables / functions.

  If there is a header file ah, ah define the variable i "int i;"; if there are simultaneous bc and cc include ah, the build error occurs when the project, why: because in fact bc and cc contain "int i ; "this statement, respectively, when the BC compile and cc," int i; "compile this statement is two, and therefore corresponds to the variable i is defined twice, so when the link error.

 

 

Guess you like

Origin www.cnblogs.com/XiaoXiaoShuai-/p/12008629.html