我不明白两者的意义。


当前回答

根据GNU C库手册(http://www.gnu.org/software/libc/manual/html_node/Header-Files.html)

在C语言中,声明仅仅提供函数或变量存在的信息并给出其类型。对于函数声明,还可以提供关于其参数类型的信息。声明的目的是允许编译器正确地处理对声明的变量和函数的引用。另一方面,定义实际上为变量分配存储空间,或者说明函数的功能。

其他回答

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

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在这个源代码中存在!!”

定义:想象一本字典,你正在查找Foo和它的实际意思。

我最喜欢的例子是int Num = 5这里你的变量是1。定义为int 2。声明为Num和3。实例化值为5。我们

定义对象的类型,可以是内置的,也可以是类或结构。 声明一个对象的名字,这样任何有名字的东西都被声明了,包括变量、函数等。

类或结构允许您在以后使用时更改对象的定义方式。例如

可以声明没有特别定义的异构变量或数组。 在c++中使用偏移量可以定义一个没有声明名称的对象。

当我们学习编程时,这两个术语经常被混淆,因为我们经常同时做这两个。

c++ 11更新

由于我没有看到与c++ 11相关的答案,这里有一个。

声明是定义,除非声明了/n:

opaque enum - enum X: int; 模板参数-模板参数- MyArray 参数声明- x和y在int add(int x, int y); 别名声明-使用IntVector = std::vector<int>; - static_assert(sizeof(int) == 4, "Yikes!") 属性声明(实现定义的) 空声明;

以上列表从c++ 03继承的附加子句:

函数声明- add in int add(int x, int y); Extern说明符包含声明或链接说明符- Extern int a;或extern "C"{…}; 类中的静态数据成员-类C中的x{静态int x;}; 类/struct声明- struct Point; typedef int int; 使用声明-使用std::cout; 使用命名空间NS;

模板声明是一种声明。如果模板声明定义了函数、类或静态数据成员,那么模板声明也是定义。

以下例子来自于区分声明和定义的标准,我发现这些例子有助于理解它们之间的细微差别:

// except one all these are definitions
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


// all these are declarations
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 N::d


// specific to C++11 - these are not from the standard
enum X : int;                           // declares X with int as the underlying type
using IntVector = std::vector<int>;     // declares IntVector as an alias to std::vector<int>
static_assert(X::y == 1, "Oops!");      // declares a static_assert which can render the program ill-formed or have no effect like an empty declaration, depending on the result of expr
template <class T> class C;             // declares template class C
;                                       // declares nothing