The difference between a variable definition and a declaration detailed parsing _c language

Source: Internet
Author: User

We are in the program design, always use the definition of variables and variable declarations, but sometimes we are not very clear about the concept, know how it is used, but do not know how to do, the following I simply put their differences are described as follows:

There are two things about the declaration of a variable:
(1) One is the need to establish storage space (definition, declaration). For example, int A has established storage space when it is declared.

(2) The other is that there is no need to create storage space (declaration). For example: extern int A, where variable A is defined in a different file.

The former is a "defined declaration (defining declaration)" or "definition", and the latter is a "referential declaration (referncing declaration)". In a broad sense, a declaration contains a definition, but not all declarations are defined, for example: int A is both a declaration and a definition. For extern A, however, it is merely a declaration and not a definition. in general, we often describe the creation of space as a "definition", and call it "declaration" without the need to create storage space. It is clear that the statement we are referring to here is narrower, that is to say, a declaration of a undefined nature.

For example: in the main function

Copy Code code as follows:

int main ()
{
extern int A; This is a declaration rather than a definition, declaring a is an external variable that has already been defined
Note: When declaring an external variable, you can remove the variable type as: extern A;
Dosth (); Execution function
}

int a;//is defined as an external variable (global variable) that defines a as an integral type.


The definition of an external variable (global variable) is not the same as the "declaration" of an external variable, and the external variable can only be defined once .Its position is outside all functions, and the external variable declaration in the same file can 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 an external variable). The system allocates storage based on the definition of an external variable, rather than on the declaration of an external variable. For external variables, initialization can only be done in the definition, not in the declaration. The function of a so-called "declaration" is to declare that the variable is an external variable that has been defined later, merely as a "declaration" for referring to the variable "in advance". extern only makes a declaration and does not define it.

The function of declaring a variable with static is two:
(1) with a static declaration for a local variable, the space allocated for the variable always exists throughout the execution of the program
(2) If the external variable is declared with static, the function of the variable is limited to this file module

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.