0

What does the "nested scope" code mean? and what is the result?

{ int x = 1; cout << x << endl; { cout << x << endl; int x = 2; cout << x << endl; } cout << x << endl; }

30th Oct 2018, 2:57 AM
林克翰
2 odpowiedzi
+ 1
It basically limits the scope of declared variables inside the curly brackets. I believe (correct me if I'm wrong), that the first cout will use the x outside the nested scope, but then a new x is declared inside, which is only available to the nested scope. The output might be: 1 1 2 1
30th Oct 2018, 4:07 AM
Zeke Williams
Zeke Williams - avatar
+ 1
Maybe this will help a bit { int x = 1; cout <<x; { int y = 2; cout << x << y;//1,2 } cout <<y; //error, y does not exist anymore }
30th Oct 2018, 4:18 AM
Vlad Serbu
Vlad Serbu - avatar