zoukankan      html  css  js  c++  java
  • what is the difference between definition and declaration in c

    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

    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

  • 相关阅读:
    nodejs中的全局函数setTimeout/clearTimeout,setInterval/clearInterval,unref/ref
    nodejs的核心对象console
    创建一个服务器,解析当前的url并根据url并作出相应的响应
    nodejs创建服务并加载一个html文件
    nodejs读文件
    Get和Post的区别
    ui-grid 网格布局--jQueryMobile
    web开发常见问题
    全选和全不选
    微信小程序-调用工具js文件/utils文件中的函数/变量
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4713247.html
Copyright © 2011-2022 走看看