Monday, 9 May 2016

Multiple declaration of global variable with same variable name

 
Guess the following output?

int a;
int a;

int main()

{
   return 0;
}



Solution: It will not cause any compilation errors in c, but not in C++. This is because multiple declaration of global variables without initialization are allowed in c, since C standard doesn’t restrict on this. But if Global variable is initialized then declaration should not repeat again.



int a; // Valid
int a=10; // Valid

//int a; - Invalid, variable already initialized

int main()
{
   return 0;
}