0

How do I call a variable that has the same name as a variable instantiated inside of an if-statement? Here's some code for reference:

int main() {
   int x = 5;
   
   if (x) {
       int x = 2;
       cout << x << endl; // should print out 2.
       cout << ::x << endl; // should print out 5.
   }

   return 0;
}

I know that the second cout line is incorrect, so what would the correct way of writing it be?

user438383
  • 5,716
  • 8
  • 28
  • 43
No So
  • 19
  • 1

0 Answers0