What is the difference between a definition and a

2018-12-30 23:00发布

The meaning of both eludes me.

22条回答
梦寄多情
2楼-- · 2018-12-30 23:11

Declaration :

int a; // this declares the variable 'a' which is of type 'int'

Thus declaration associates the variable with a type.

Following are some examples of declaration.

int a;
float b;
double c;

Now function declaration :

int fun(int a,int b); 

Note the semicolon at the end of function so it says it is only a declaration. Compiler knows that somewhere in the program that function will be defined with that prototype. Now if the compiler gets a function call something like this

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

Compiler will throw an error saying that there is no such function. Because it doesn't has any prototype for that function.

Note the difference between two programs.

Program 1

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

In this, print function is declared and defined as well. Since function call is coming after the definition. Now see the next program.

Program 2

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

It is essential because function call precedes definition so compiler must know whether there is any such function. So we declare the function which will inform the compiler.

Definition :

This part of defining a function is called Definition. It says what to do inside the function.

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

Now with the variables.

int a; //declaration
a=10; //definition 

Some times declaration and definition are grouped into a single statement like this.

int a=10;
查看更多
看风景的人
3楼-- · 2018-12-30 23:12

From wiki.answers.com:

The term declaration means (in C) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user defined type or function in your program. No space is reserved in memory for any variable in case of declaration. However compiler knows how much space to reserve in case a variable of this type is created.

for example, following are all declarations:

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

Definition on the other hand means that in additions to all the things that declaration does, space is also reserved in memory. You can say "DEFINITION = DECLARATION + SPACE RESERVATION" following are examples of definition:

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

see Answers.

查看更多
只若初见
4楼-- · 2018-12-30 23:12

Couldnt you state in the most general terms possible, that a declaration is an identifier in which no storage is allocated and a definition actually allocates storage from a declared identifier?

One interesting thought - a template cannot allocate storage until the class or function is linked with the type information. So is the template identifier a declaration or definition? It should be a declaration since no storage is allocated, and you are simply 'prototyping' the template class or function.

查看更多
梦寄多情
5楼-- · 2018-12-30 23:13

C++11 Update

Since I don't see an answer pertinent to C++11 here's one.

A declaration is a definition unless it declares a/n:

  • opaque enum - enum X : int;
  • template parameter - T in template<typename T> class MyArray;
  • parameter declaration - x and y in int add(int x, int y);
  • alias declaration - using IntVector = std::vector<int>;
  • static assert declaration - static_assert(sizeof(int) == 4, "Yikes!")
  • attribute declaration (implementation-defined)
  • empty declaration ;

Additional clauses inherited from C++03 by the above list:

  • function declaration - add in int add(int x, int y);
  • extern specifier containing declaration or a linkage specifier - extern int a; or extern "C" { ... };
  • static data member in a class - x in class C { static int x; };
  • class/struct declaration - struct Point;
  • typedef declaration - typedef int Int;
  • using declaration - using std::cout;
  • using directive - using namespace NS;

A template-declaration is a declaration. A template-declaration is also a definition if its declaration defines a function, a class, or a static data member.

Examples from the standard which differentiates between declaration and definition that I found helpful in understanding the nuances between them:

// 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
查看更多
君临天下
6楼-- · 2018-12-30 23:13

The concept of Declaration and Definition will form a pitfall when you are using the extern storage class because your definition will be in some other location and you are declaring the variable in your local code file (page). One difference between C and C++ is that in C you the declarations are done normally at the beginning of a function or code page. In C++ it's not like that. You can declare at a place of your choice.

查看更多
柔情千种
7楼-- · 2018-12-30 23:14

There are interesting edge cases in C++ (some of them in C too). Consider

T t;

That can be a definition or a declaration, depending on what type T is:

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".

In C++, when using templates, there is another edge case.

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

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

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

The last declaration was not a definition. It's the declaration of an explicit specialization of the static member of X<bool>. It tells the compiler: "If it comes to instantiating X<bool>::member, then don't instantiate the definition of the member from the primary template, but use the definition found elsewhere". To make it a definition, you have to supply an initializer

template<>
int X<bool>::member = 1; // definition, belongs into a .cpp file.
查看更多
登录 后发表回答