C / C ++ definitions and declarations explain (rpm)

 Transfer: https: //www.cnblogs.com/Champion-L/p/7202714.html

 

  1. Definition of variable declarations

  Declare variables there are two cases:

  One is the need for storage space. For example: int a. When the statement was already established storage space. Such a declaration is "a defining statement (defining declaration)", that is, we usually say "definition."

  Another is the need to establish storage space, just tell the compiler has a variable defined elsewhere before. For example: extern int a. Wherein a variable is defined elsewhere. Such a declaration is "quoted statement (referncing declaration)", that is, we usually call "statement."

 

  From a broad perspective, the declaration contains definitions, but not all of the statements are defined. That is, both the definition and the definition statement is a statement, and statement just quoted a statement. For example: int a. It is a statement, but it is also defined. However, for extern int a concerned, it's just a statement not a definition.

 

  Under normal circumstances we so often described, to declare the establishment of a space called the "definition", and do not need to create a volume called "statement." Obviously this statement refers to a relatively narrow range, that is a reference of the declaration.

 

  example:

  int a; // defining declaration, allocate storage space, the initial uncertainty

  int b = 0; // define statement, allocate storage space, given the initial value

  extern int c; // quoted statement, does not allocate storage space, just tell the compiler that the variable is already defined elsewhere over

 

  2. Definition of function declaration

  Definition and function declaration is the same, the definition needs to allocate storage space; a statement just tells the compiler that this function is already defined elsewhere.

  Function definitions and declarations of better distinction. There is the definition of the function body, with no function body shall be declared.

 

  example:

  //statement

  

double sqrt(double x);

 

  //定义

  double sqrt(double x)

  {

          return x*x;

  }

 

 

  3. Define the difference between the declared

  (1) the need to allocate storage space.

  (2) may be declared in a scope repeated, but can not be redefined. This is determined by (1) can be repeated several times to tell the compiler to a variable, the function has been defined elsewhere, but can not be repeated many times to let the compiler for the same variable, the function assigned different storage space.

 

 

 

Guess you like

Origin www.cnblogs.com/jasonLiu2018/p/11495286.html