+ 1

Scope of variable

Hi int x; void fun() { int x = 2; { int x = 3; cout << ::x << endl; } } Above code has int x as global variable. From fun function, ::X will print value of global variable and x will print value local to scope of {} which is 3. How can I print value of X as 2 from a local scope enclosed inside {} ?

20th Apr 2021, 9:58 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
I don't think it's possible. The variable x from main's scope is "shadowed" as they say.
20th Apr 2021, 10:04 AM
Schindlabua
Schindlabua - avatar