我不明白两者的意义。


当前回答

定义:

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);
}

其他回答

定义意味着实际编写的函数,声明意味着简单的声明函数 如。

void  myfunction(); //this is simple declaration

and

void myfunction()
{
 some statement;    
}

这是函数myfunction的定义

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.

为了理解名词,我们先来看看动词。

声明- - - - - - 宣布:正式宣布;宣告

定义- - - - - - 清晰完整地显示或描述(某人或某事

所以,当你声明某物时,你只需告诉它是什么。

// declaration
int sum(int, int);

这一行声明了一个名为sum的C函数,它接受两个int类型的参数并返回一个int。但是,您还不能使用它。

当你提供它的实际工作方式时,这就是它的定义。

// definition
int sum(int x, int y)
{
    return x + y;
}

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

宣言

声明告诉编译器a 程序元素或名称存在。一个 声明引入一个或多个 命名到程序中。声明可以 在一个程序中出现一次以上。 因此,类,结构, 枚举类型等 用户定义的类型可以声明为 每个编译单元。

定义

定义指定哪些代码或数据 名称描述。名称必须为 在使用之前声明。