20 Nov 2019

  • November 20, 2019
  • Amitraj
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.


//Write a C++ program for declaration & initialization of variable

#include <iostream>
using namespace std;
int main ()
{
    int sum;     //Variable declaration
    int a = 10;  //Variable declaration & initialization
    int b = 5;   //Variable declaration & initialization
    sum = a + b;
    cout << "Addition is:" << sum << endl;
    return 0;
}

OUTPUT:

Addition is: 15

Translate

Popular Posts