Chinaunix首页 | 论坛 | 博客
  • 博客访问: 115383
  • 博文数量: 8
  • 博客积分: 2560
  • 博客等级: 少校
  • 技术积分: 192
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-15 09:22







分类: C/C++

2008-10-30 13:00:07

昨天,有同事问我“C 语言中,定义和声明有什么不同”,我凭感觉说“定义是告诉编译器要有这样一个东西,声明是告诉编译器有这样一个东西”。
晚上看《Compilers Principles, Techniques, and Tools》第二版的 1.6.5 小节时候恰巧看到:
“The apparently similar terms "declaration" and "definition" for programming-language concepts are actually quite different. Declarations tell us about the types of things, while definitions tell us about their values. Thus, int i is a declaration of i, while i = I is a definition of i.
The difference is more significant when we deal with methods or other procedures. In C++, a method is declared in a class definition, by giving the types of the arguments and result of the method (often called the signature for the method. The method is then defined, i.e., the code for executing the method is given, in another place. Similarly, it is common to define a C function in one file and declare it in other files where the function is used.”
于是,一时兴起就找到《The C Programing Language》翻翻,终于在 1.10 小节找到:
“You should note that we are using the words definition and declaration carefully when we refer to external variables in this section. "Definition" refers to the place where the variable is created or assigned storage; "declaration" refers to places where the nature of the variable is stated but no storage is allocated.”
阅读(1658) | 评论(0) | 转发(0) |