Question:

What is the output of the following code snippet? verbatim #include <iostream> using namespace std; int main() int x = 5; int y = x++; cout << y; return 0; verbatim

Show Hint

Remember the difference between pre-increment (`++x`) and post-increment (`x++`). Pre-increment: increment first, then use the value. Post-increment: use the value first, then increment.
Updated On: Jul 2, 2026
  • 5
  • 6
  • 0
  • Compilation error
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Let's trace the execution of the code step-by-step.
1. int x = 5;`: An integer variablex` is declared and initialized with the value 5.
2. int y = x++;`: This line involves the post-increment operator (`++` after the variable). The post-increment operator works in two steps: first, it uses the current value of the variable in the expression, and then it increments the variable. So, the current value ofx` (which is 5) is assigned to the variabley`. After the assignment, the value ofx` is incremented to 6.
3. At this point,y` holds the value 5, andx` holds the value 6.
4. cout << y;`: The value of the variabley` is printed to the console.
Therefore, the output of the code will be 5.
Was this answer helpful?
0
0