C Language Declaration and definition

Source: Internet
Author: User

A long time no contact with C language, today encountered a statement and definition of the problem, and then found the following explanation:

The Declaration (Declaration) specifies the identifier of a variable that describes the type of the variable, whether it is a type or an object, or a function. Declaration, used by the compiler (compiler) to identify the entity referenced by the variable name. These are the declarations:

extern int bar;
extern int g (int, int);
Double f (int, double); For function declarations, the extern keyword can be omitted.
class Foo; class declaration, which cannot be preceded by class.


A definition is an implementation or instantiation of a declaration. The connector (linker) needs it (defined) to refer to the memory entity. The definition corresponding to the above statement is as follows:
int bar;
int g (int lhs, int rhs) {return LHS*RHS;}
Double f (int i, double d) {return i+d;}
class Foo {};//foo already has its own memory, and against the above two functions, you should understand the usefulness of {}?


In any case, defining an operation can only be done once. If you forget to define some variables that you have already declared, or those that are referenced in some places, then the connector linker is unaware that these references are connected to that block of memory. Then you will report missing symbols such a mistake. If you define a variable more than once, the connector does not know which memory to connect the reference to, and then it will report duplicated symbols such an error. The above symbols actually refers to the defined variable name, which is the memory block of its identity.


Summarize
If it is just to provide the compiler with a reference identity, so that the compiler can know that there is a reference to that reference, it can be used to refer to an entity (but not the process of allocating a specific block of memory to the entity) for the declaration. If the operation is able to specify a specific piece of memory for the reference, so that the reference can only correctly correspond to a piece of memory in the link phase, the operation is defined.


The declaration is intended to allow the compiler to correctly handle references to declared variables and functions. A definition is a process of allocating memory to a variable, or a function that is specific to what it is used for.

C Language Declaration and definition

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.