What is the difference between a definition and a declaration in C++?

In C++, declaration and definition are often confused. A 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 the declaration.

The Definition on the other hand means that in additions to all the things that declaration does, space is additionally reserved in memory. You can say "DEFINITION = DECLARATION + SPACE RESERVATION".

Following are examples of declarations −

extern int a;                           // Declaring a variable a without defining it
struct _tagExample { int a; int b; };   // Declaring a struct
int myFunc (int a, int b);              // Declaring a function

While following are examples of definition −

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

Passionate, Curious and Enthusiastic.