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

全部博文(8)

文章存档

2012年(1)

2009年(2)

2008年(5)

我的朋友

分类: 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.”
 
仔细体会,你会发觉两个解释实际上稍有不同。
 
问这个问题的人似乎有点……
 
 
阅读(1758) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~