我不明白两者的意义。


当前回答

声明引入标识符并描述其类型,可以是类型、对象或函数。声明是编译器接受对该标识符的引用所需要的。这些是声明:

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 type declarations

定义实际上实例化/实现了这个标识符。它是链接器为了将引用链接到这些实体所需要的。这些是对应于上述声明的定义:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

定义可以用在声明的地方。

标识符可以根据需要任意声明。因此,以下代码在C和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);

但是,它必须只定义一次。如果您忘记定义某个已经声明和引用的东西,那么链接器就不知道要链接到什么地方,并抱怨缺少符号。如果定义一个东西不止一次,那么链接器就不知道要链接引用哪个定义,并抱怨重复的符号。


由于在c++中什么是类声明和类定义的争论不断出现(在对其他问题的回答和评论中),我将在这里粘贴一段来自c++标准的引用。 在3.1/2,c++ 03说:

声明是定义,除非它[…是类名声明[…]。

3.1/3然后给出了几个例子。其中:

[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b [...]
struct S; // declares S
—end example

总结一下:c++标准考虑结构体x;是一个声明和结构x {};一个定义。(换句话说,“前向声明”是用词不当,因为在c++中没有其他形式的类声明。)

感谢litb (Johannes Schaub)在他的回答中找出了实际的章节和诗句。

其他回答

c++中有一些有趣的边缘情况(在C中也有一些)。考虑

T t;

它可以是一个定义,也可以是一个声明,取决于T是什么类型:

typedef void T();
T t; // declaration of function "t"

struct X { 
  T t; // declaration of function "t".
};

typedef int T;
T t; // definition of object "t".

在c++中,当使用模板时,还有另一种边缘情况。

template <typename T>
struct X { 
  static int member; // declaration
};

template<typename T>
int X<T>::member; // definition

template<>
int X<bool>::member; // declaration!

最后一个声明不是定义。它声明了X<bool>的静态成员的显式特化。它告诉编译器:“如果要实例化X<bool>::member,那么不要从主模板实例化成员的定义,而是使用在其他地方找到的定义”。要使其成为定义,必须提供初始化式

template<>
int X<bool>::member = 1; // definition, belongs into a .cpp file.

从wiki.answers.com:

术语声明(在C语言中)意味着你告诉编译器关于类型,大小,如果是函数声明,任何变量的参数的类型和大小,或者程序中用户定义的类型或函数。在声明时,内存中不为任何变量保留空间。然而,编译器知道在这种类型的变量被创建的情况下要保留多少空间。

例如,以下是所有的声明:

extern int a; 
struct _tagExample { int a; int b; }; 
int myFunc (int a, int b);

另一方面,定义意味着除了声明所做的所有事情之外,内存中还保留了空间。你可以说“DEFINITION = DECLARATION + SPACE RESERVATION”,下面是定义的例子:

int a; 
int b = 0; 
int myFunc (int a, int b) { return a + b; } 
struct _tagExample example; 

看到答案。

c++标准3.1节:

声明将名称引入到翻译单元中,或者重新声明前一个翻译单元中引入的名称 声明。声明指定这些名称的解释和属性。

下一段声明(强调是我的)声明是一种定义,除非……

... 它声明一个函数而不指定函数体:

void sqrt(double);  // declares sqrt

... 它在类定义中声明一个静态成员:

struct X
{
    int a;         // defines a
    static int b;  // declares b
};

... 它声明了一个类名:

class Y;

... 它包含没有初始化式或函数体的extern关键字:

extern const int i = 0;  // defines i
extern int j;  // declares j
extern "C"
{
    void foo();  // declares foo
}

... Or是类型定义或using语句。

typedef long LONG_32;  // declares LONG_32
using namespace std;   // declares std

现在,理解声明和定义之间的区别很重要的一个重要原因是:一个定义规则。c++标准第3.2.1节:

任何翻译单元都不能包含任何变量、函数、类类型、枚举类型或模板的多个定义。

定义:

extern int a;      // Declaration 
int a;             // Definition
a = 10             // Initialization
int b = 10;        // Definition & Initialization

定义将变量与类型关联起来并分配内存,而声明只指定类型而不分配内存。当您希望在定义之前引用变量时,声明更有用。

*不要混淆定义和初始化。两者是不同的,初始化给变量赋值。参见上面的例子。

下面是一些定义的例子。

int a;
float b;
double c;

现在函数声明:

int fun(int a,int b); 

注意函数末尾的分号,所以它表示它只是一个声明。编译器知道在程序的某个地方,该函数将被定义为原型。现在,如果编译器得到一个这样的函数调用

int b=fun(x,y,z);

编译器将抛出一个错误,指出没有这样的函数。因为它没有那个函数的原型。

注意两个程序之间的区别。

程序1

#include <stdio.h>
void print(int a)
{
     printf("%d",a);
}
main()
{
    print(5);
}

其中,print函数也被声明和定义。因为函数调用在定义之后。现在看下一个节目。

项目2

 #include <stdio.h>
 void print(int a); // In this case this is essential
 main()
 {
    print(5);
 }
 void print(int a)
 {
     printf("%d",a);
 }

这是必要的,因为函数调用先于定义,所以编译器必须知道是否有这样的函数。因此,我们声明了一个函数,该函数将通知编译器。

定义:

定义函数的这一部分称为定义。它告诉我们在函数中要做什么。

void print(int a)
{
    printf("%d",a);
}

《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]