Monday, 4 July 2016

Can we initialize the variable with same variable while declaring?

 
Guess the output?

#include<iostream>
int main()
{
double *ptr=ptr;
int a=a,b=10,c=b;
std::cout<<static_cast<bool>(ptr)<<std::endl;
return 0;
}


OUTPUT:

Program compiles without any compilation error. And the output of the program is undefined.

EXPLANATION:

Standard allows to declare the variable with same name. It is perfectly valid as per standard.
So what happens when we initialize like that?
Nothing will happen, only garbage value will be stored.

And the output of the program is undefined because garbage value can be anything.