Q. What is the output of following program?
#include <iostream>
using namespace std;
// main() is where program execution begins.
int g,t;
float f,h;
int main()
{
int a,b;
float d,l;
cout<<"GLobal Int g="<<g<<endl;
cout<<"GLobal Int t="<<t<<endl;
cout<<"Local Int a="<<a<<endl;
cout<<"Local Int b="<<b<<endl;
cout<<"Local float d="<<d<<endl;
cout<<"Local float l="<<l<<endl;
cout<<"Global float f="<<f<<endl;
cout<<"Global float h="<<h<<endl;
return 0;
}
Answer:-
Explanation :
Local variable is not defined by the system and need to be initialized. I not initialized then it can take any garbage value but the Global variables are initialized automatically by the system when we define them.
#include <iostream>
using namespace std;
// main() is where program execution begins.
int g,t;
float f,h;
int main()
{
int a,b;
float d,l;
cout<<"GLobal Int g="<<g<<endl;
cout<<"GLobal Int t="<<t<<endl;
cout<<"Local Int a="<<a<<endl;
cout<<"Local Int b="<<b<<endl;
cout<<"Local float d="<<d<<endl;
cout<<"Local float l="<<l<<endl;
cout<<"Global float f="<<f<<endl;
cout<<"Global float h="<<h<<endl;
return 0;
}
Answer:-
GLobal Int g=0 //always 0 if global int GLobal Int t=0 //always 0 if global int Local Int a=0 //any garbage value if local and not assigned , in most case it can take 0 if only one int or one float Local Int b=4196896 //any garbage value if local and not assigned Local float d=0 //any garbage value if local and not assigned Local float l=5.88211e-39 //any garbage value if local and not assigned Global float f=0 //always 0 if global float Global float h=0 //always 0 if global float
Explanation :
Local variable is not defined by the system and need to be initialized. I not initialized then it can take any garbage value but the Global variables are initialized automatically by the system when we define them.