Differences between variable definition and Declaration

Source: Internet
Author: User

Differences between variable definition and Declaration

(Reprinted)

In program design, we always use the definition of variables and the declaration of variables. Sometimes we are not very clear about this concept and know how to use it, but I don't know how it will take for a while. Below I will simply introduce their differences as follows:

There are two scenarios for variable declaration:
(1) One is to create a bucket (Definition and Declaration ). For example, int A has created a bucket When declaring it.
(2) There is no need to create a bucket (Declaration ). For example, extern int A, where variable A is defined in other files.
The former is "defining Declaration" or "Definition", while the latter is "referncing Declaration )". In a broad sense, declarations contain definitions, but not all declarations are definitions. For example, int A is both declaration and definition. However, for extern A, it only declares that it is not a definition.In general, we often describe the definition of a bucket, rather than the definition of a bucket ".Obviously, the declaration we refer to here is narrow in scope, that is, a non-defined declaration.

For example, in the main function
Int main ()
{
Extern int A; // This is a declaration, not a definition. Declare a as an external variable that has already been defined.
// Note: When declaring an external variable, you can remove the variable type, for example, extern;
Dosomething (); // execute a function
}

Int A; // is the definition. It defines an external variable (global variable) whose A is an integer)

The "Definition" of an external variable (global variable) is different from the "Declaration" of an external variable. An external variable can be defined only once.It is located outside all functions, and the external variable declaration in the same file can be multiple times, it can be within the function (which function should be declared in that function) it can also be outside the function (before the external variable's definition point ). The system allocates storage space according to the definition of external variables (rather than the declaration of external variables. For external variables, initialization can only be performed in "Definition", rather than in "Declaration. The so-called "Declaration" serves to declare that the variable is an external variable that has been defined later, it is just a "Declaration" made to "reference this variable in advance. Extern is only declared and not defined.

Using static to declare a variable has two functions:
(1) If a local variable is declared as static, the space allocated for the variable will always exist throughout the execution period of the program.
(2) When an external variable is declared as static, the function of this variable is limited to the module of this file.

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.