Differences in definition and declaration of variables (collation)

Source: Internet
Author: User

There are two scenarios for declaring a variable:

1, one is the need to set up storage space. For example, int A has established a storage space at the time of declaration.

2, there is no need to set up storage space. For example, the variable a in extern int A is defined in another file.

A declaration is a name-identifier that is introduced to the compiler. It tells the compiler "This function or variable can be found somewhere, what it looks like".

The definition is, "create a variable here" or "Create a function here". It allocates storage space for the name. Whether you define a function or a variable, the compiler allocates storage space for them at the defining point. For variables, the compiler determines the size of the variable, then opens up space in memory to hold its data, and for the function, the compiler generates code that eventually consumes some memory.

In short: Make the declaration of building space a "definition" and make it a "declaration" without having to create a storage space.

The Declaration and definition (initialization) of a basic type variable is generated at the same time, whereas for an object, the declaration and definition are separate.

For example: Class A

Suppose A; is a declaration that tells the compiler that a is an object variable of Class A, but does not initialize;

Suppose later a=new a (); This is initialization, allocating space.

(The purpose of our statement is to use it in advance, that is, to use it before the definition, assuming that there is no need to declare it in advance, that is, the variables are so, so the declaration does not allocate storage space, and the storage space is allocated only when it is 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 is always present during the entire program's run time.

(2) If the external variable is declared with static, the function of the variable is limited to this file module.

Add:

What is a definition? What is a statement? What is the difference between them?

The so-called definition is (the compiler) creates an object, allocates a piece of memory for the object, and gives it a name, which is what we often call variable names or object names.

The declaration has a 2-heavy meaning:

(1) Tell the compiler that the name has been matched to a piece of memory, and the following code is used to define the variable or object elsewhere. Declarations can occur multiple times.

(2) Tell the compiler that the name has been reserved, and that it can no longer be used as a variable name or object name elsewhere.

The most important differences between definitions and declarations are:

Defines the creation object and allocates memory for this object, declaring no memory allocated.

References:

Http://hi.baidu.com/kingcham/blog/item/71740f12ab31012fdc54015d.html

http://apps.hi.baidu.com/share/detail/20436534

Deep anatomy of C language, Beijing University of Aeronautics and Astronautics Press Chengzhong P2-P3


Differences in variable definition and declaration (collation)

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.