what is the difference between definition and declaration in c
A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to 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 declarations
class foo; // no extern allowed for class declarations
A definition actually instantiates/implements this identifier. It's what the linker needs in 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 above
extern 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 missing symbols. 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
相关文章
- 安卓 android studio 报错 Unknown host 'jcenter.bintray.com'. You may need to adjust the proxy settings in Gradle.
- HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
- 创龙OMAPL138开发板测试Device is held in reset. Take the device out of reset, and retry the operation.
- [React] Use React.ReactNode for the children prop in React TypeScript components and Render Props
- [ReactVR] Animate Text, Images, Views, and 3D Elements Using the Animated Library in React VR
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
- [React] Use React.ReactNode for the children prop in React TypeScript components and Render Props
- [HTML5] Add an SVG Image to a Webpage and Get a Reference to the Internal Elements in JavaScript
- [TypeScript] Deeply mark all the properties of a type as read-only in TypeScript
- AndroidStudio3.0 注解报错Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.
- What is the usage of Transfer field in Text determine procedure customizing
- 【Codeforces 1083A】The Fair Nut and the Best Path
- Android问题笔记 - Studio 编译报错 The minCompileSdk (33) specified in a dependency‘s AAR metadata
- 解决The type or namespace name 'XXXX' does not exist in the namespace 'XXXXXXXXX' 的错误
- Matlab:成功解决In an assignment A(I)=B,the number of elements in B and I must be the same
- Paper:LSTM之父眼中的深度学习十年简史《The 2010s: Our Decade of Deep Learning / Outlook on the 2020s》的解读
- 【异常】Maven提示 repository element was not specified in the POM inside distributionManagement element
- 已解决FutureWarning: The default value of regex will change from True to False in a future version. In
- Unable to update the EntitySet 'T_JsAPI' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
- [TroubleShooting] The remote copy of database xx has not been rolled forward to a point in time
- 3GPP TS 23502-g40 中英文对照 | 4.17.4 NF/NF service discovery by NF service consumer in the same PLMN
- 解决办法:The name 'Response' does not exist in the current context
- 【异常】FlinkException: The module flink-runtime-web could not be found in the class path
- Enabling GPUs in the Container Runtime Ecosystem
- 解决办法:The name 'Response' does not exist in the current context