我不明白两者的意义。


当前回答

《K&R》(第二版)中有一些非常明确的定义;这有助于把它们放在一个地方,并作为一个整体来阅读:

"Definition" refers to the place where the variable is created or assigned storage; "declaration" refers to the places where the nature of the variable is stated but no storage is allocated. [p. 33] ... It is important to distinguish between the declaration of an external variable and its definition. A declaration announces the properties of a variable (primarily its type); a definition also causes storage to be set aside. If the lines int sp; double val[MAXVAL] appear outside of any function, they define the external variables sp and val, cause storage to be set aside, and also serve as the declaration for the rest of that source file. On the other hand, the lines extern int sp; extern double val[]; declare for the rest of the source file that sp is an int and that val is a double array (whose size is determined elsewhere), but they do not create the variables or reserve storage for them. There must be only one definition of an external variable among all the files that make up the source program. ... Array sizes must be specified with the definition, but are optional with an extern declaration. [pp. 80-81] ... Declarations specify the interpretation given to each identifier; they do not necessarily reserve storage associated with the identifier. Declarations that reserve storage are called definitions. [p. 210]

其他回答

从c++标准文档中添加定义和声明示例(来自3.1节声明和定义)

定义:

int a;                       // defines a
extern const int c = 1;      // defines c
int f(int x) { return x+a; } // defines f and defines x
struct S { int a; int b; };  // defines S, S::a, and S::b
struct X {                   // defines X
    int x;                   // defines non-static data member x
    static int y;            // DECLARES static data member y
    X(): x(0) { }            // defines a constructor of X
};
int X::y = 1;                // defines X::y
enum { up, down };           // defines up and down
namespace N { int d; }       // defines N and N::d
namespace N1 = N;            // defines N1
X anX;                       // defines anX

声明:

extern int a;                 // declares a
extern const int c;           // declares c
int f(int);                   // declares f
struct S;                     // declares S
typedef int Int;              // declares Int
extern X anotherX;            // declares anotherX
using N::d;                   // declares d

经验法则:

声明告诉编译器如何解释内存中的变量数据。这是每个访问都需要的。 定义保留内存以使变量存在。这必须在第一次访问之前发生一次。

声明意味着给变量命名和类型(在变量声明的情况下),例如:

int i;

或者将名称、返回类型和参数类型赋给一个没有函数体的函数(在函数声明的情况下),例如:

int max(int, int);

而定义意味着给变量赋值(在变量定义的情况下),例如:

i = 20;

或者为函数提供/添加函数体(功能)被称为函数定义,例如:

int max(int a, int b)
{
   if(a>b)   return a;
   return b;  
}

许多时间声明和定义可以一起完成:

int i=20;

and:

int max(int a, int b)
{
    if(a>b)   return a;
    return b;    
} 

在上述情况下,我们定义并声明变量i和函数max()。

声明:“在某处,存在一个foo。”

定义:“…就是这儿!”

声明说"这个东西存在于某个地方"

int sampleFunc(); // function
extern int car;  // variable

定义说,这个东西存在于这里;为它留下记忆"

int sampleFunc() {} // function
int car; // variable

在定义对象时,初始化是可选的,并表示“这是这个东西的初始值”:

int car = 0; // variable