How to Declare Variables in C++


In C we have to declare all the variables at the starting of any function or scope. However in C++ we can declare variable anywhere in the scope before its first use. There is no need to declare all the variables at the starting of the scope.

Example:
int main()
{
int n,sum=0;
for (int i = 1 ; i<=10;i++)
{
cin>>n;
sum=sum+n;
}
float average = sum/10;
}


     

Dynamic Initialization of Variables


The process of initializing variable at the time of its declaration at run time is known as dynamic initialization of variable.
Thus in dynamic initialization of variable a variable is assigned value at run time at the time of its declaration.

Example:
int main()
{
int a;
cout << “Enter Value of a”;
cin >> a;
int cube = a * a * a;
}

In above example variable cube is initialized at run time using expression a * a * a at the time of its declaration.

Download Projects


Download Programs