222k views
1 vote
What is output by the following code?

int main() {
int x = 10;
int y = 77;
if (y > 50) {
int x = 400;
cout << x << " ";
}
cout << x << endl;
}

User Psicopoo
by
7.6k points

1 Answer

6 votes

Final answer:

The code will output 400 and 10.

Step-by-step explanation:

The code will output:

  1. 400
  2. 10

The reason is that inside the if statement, a new variable x is declared and assigned the value 400. This variable is local to the if block and does not affect the value of the x variable declared outside the if block. Therefore, when the code prints the value of x after the if statement, it will print the original value of 10.

User StaticVariable
by
8.7k points