CPP Variable Declaration & Initialization

Variable declaration and initialization

C++ is a strongly-typed language, and requires every variable to be declared with its type before its first use.
This informs the compiler the size to reserve in memory for the variable and how to interpret its value.
The syntax to declare a new variable in C++ is straightforward: we simply write the type followed by the variable name (i.e., its identifier).

Syntax:

char c;     //character variable declaration.
int area;   //integer variable declaration.
float num;  //float variable declaration.

These are three valid declarations of variables.

  • The first one declares a variable of type char with the identifier c.
  • The second is declares a variable of type int with the identifier area.
  • The third one declares a variable of type float with the identifier num.

Once declared, the variables c, area and num can be used within the rest of their scope in the program.


Declaring More than one Variable

If declaring more than one variable of the same type, they can all be declared in a single statement by separating their identifiers with commas.

int a, b, c;    //more than one variable declaration.

This declares three variables (a, b and c), all of them of type int, and has exactly the same meaning as:

int a;  //integer variable declaration.
int b;  //integer variable declaration.
int c;  //integer variable declaration.

Initialization of variables

When the variables in the example above are declared, they have an undetermined or garbage value until they are assigned a value for the first time. But it is possible for a variable to have a specific value from the moment it is declared. This is called the initialization of the variable.
In C++, there are same ways to initialize variables as in C Language.

Syntax:

type identifier = initial_value;

Example:

int a = 10;  //integer variable declaration & initialization.

Practical:

//Write a CPP program for declaration & initialization of variable
#include <iostream.h>
int main ()
{
    int sum;    //Variable declaration
    int a = 10; //Variable declaration & initialization
    int b = 5;  //Variable declaration & initialization
    ans = a + b;
    cout << "Addition is:" << ans << endl;
    return 0;
}

Output:

Command Prompt
Addition is: 15

Point To Remember

  • A variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable.
  • A variable declaration has its meaning at the time of compilation only, compiler needs actual variable declaration at the time of linking of the program.
  • A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program.
  • You will use extern keyword to declare a variable at any place.
  • Though you can declare a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.
  • Same concept applies on function declaration where you provide a function name at the time of its declaration and its actual definition can be given anywhere else.

Share This Page to Download:

Share to Download

Share This Page on:


Ask Question