How to declare a variable in C++?

C++ProgrammingObject Oriented Programming

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 a 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;
raja
Published on 16-Feb-2018 06:06:11
Advertisements