Declaration and definition of C language

Source: Internet
Author: User

In program design, the definition of variables and the declaration of variables are used all the time, but sometimes we are not very clear about the concept, know how it is used, but do not know how a thing.

Below I will simply introduce their differences as follows:

There are two scenarios for declaring a variable :

(1) One is the need to create a storage space (definition, declaration).

For example, int A has established storage space at the time of declaration.

(2) The other is that there is no need to create a storage space (declaration).

For example: extern int a where variable A is defined in another file.

Analysis: The former is a "defined declaration (defining declaration)" or "definition", which is a "declarative declaration (Referncing declaration)".

In broad terms, declarations contain definitions, but not all declarations are definitions.

For example: int A It is both a declaration and a definition. For extern A, however, it is simply a declaration, not a definition.

In general, we often describe the declaration of space-building as a "definition" and a "declaration" that does not require the creation of storage space. It is clear that the declaration we are referring to here is narrower, that is to say, a declaration of a non-definition nature.

For example: in the main function

 int  A; //  is a definition that defines a global variable for integral type  int   Main () { //  This is a declaration, not a definition, stating that A is an external variable that has already been defined  //  extern  int   //  Execute function   dosomething ();  return  0     

The definition of a global variable is not the same as the declaration of a global variable, the definition of a global variable can only be once, its position is outside all functions, and the global variable declaration in the same file could be multiple times. It can be within a function (which function is declared in that function) or outside the function (before the definition point of the global variable). The system allocates storage space based on the definition of the global variable (not the declaration of the Global external variable). for global variables, initialization can only be done in "definition" , not in "declarations ."

The so-called "declaration", whose function is to declare that the variable is an external variable that has been defined at a later time, is simply a "declaration" for "advance" reference to the variable. extern is declared only and is not defined.

Using static to declare a variable has two functions:

(1) For a local variable with a static declaration, the space allocated for the variable will always exist throughout the execution period of the program, local scope, global lifetime.

(2) The global variable is declared with static, then the function of the variable is limited to this file module, the global scope, the global lifetime.

Declaration and definition of C language

Related Article

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.