What is the difference between definition and declaration in c

Source: Internet
Author: User

A declaration introduces an identifier and describes its type, be it a type, object, or function.A declaration is what the compiler needsTo accept references to that identifier. These are declarations:

extern int bar;extern int g(int, int);double f(int, double); // extern can be omitted for function declarationsclass foo; // no extern allowed for class declarations

A definition actually instantiates/implements this identifier.It's what the linker needsIn order to link references to those entities. These are definitions corresponding to the above declarations: [a definition allocate space for the identifier // myself]

int bar;    [someone said it is not only a definition but also a declaration]int g(int lhs, int rhs) {return lhs*rhs;}double f(int i, double d) {return i+d;}class foo {};

A definition can be used in the place of a declaration.

An identifier can be declared as often as you want. Thus, the following is legal in C and C ++:

double f(int, double);double f(int, double);extern double f(int, double); // the same as the two aboveextern double f(int, double);

However, it must be defined exactly once. if you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missymsing Bols. if you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.




[From website] http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration

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.