Declaration and definition of variables in c and c++

  • 2020-04-02 01:45:03
  • OfStack

The declaration of either a function or a variable is to tell the compiler that I'm going to use that variable or function, for type checking. The compiler doesn't allocate any memory at definition time,

For example, the following function:


void func() {
      int a ;
      int b = 0 ;
      a = 0 ;
}

When the function executes to int a; This is a declaration that the compiler will not allocate memory space for. When we go to a = 0; This is a definition for which the compiler allocates memory space. So a declaration does not have to be a definition, a definition must be a definition, and it can contain a declaration. But this affirmation is only a statement:
Extern int   A;

Therefore:

Declare as a compilation service for type checking;
Definition allocates space at run time, cannot be defined repeatedly, and has declarative capabilities.


Related articles: