How to access global and local of same variables and display both, write a Cpp code?

All QuestionsCategory: Cpp LanguageHow to access global and local of same variables and display both, write a Cpp code?
Chetan Shidling Staff asked 5 years ago

info

1 Answers
Chetan Shidling Staff answered 5 years ago

Code

#include <iostream>
using namespace std;
int a=5;// global variable
int main()
{
int b=3;//local variable
cout<<“value of b:”<<b;
cout<<“\nGlobal value of a:”<<a;
{
int a = 8;
cout<<“\nvalue of a:”<<a;
}
cout<<“\nvalue of a is:”<<a;
return 0;
}

Output:

value of b:3
Global value of a:5
value of a:8
value of a is:5