What will be the output of the code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What will be the output of the code?

int x = 5; cout << ++x + x +x-x; // I know the answer, but I donā€™t understand why the answer is this.

5th Mar 2021, 12:22 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
5 Answers
+ 2
++x means increment x by 1 then use its value. So, x = 6 6 + 6 + 6 - 6 = 12
5th Mar 2021, 12:43 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
TahitišŸ· Short description; it is a program or collection of tools that takes your written readable code and converts it into runnable machine code, usually in the form of an executable file (exe on Windows). https://en.m.wikipedia.org/wiki/Compiler https://www.stroustrup.com/compilers.html
5th Mar 2021, 1:20 AM
ChaoticDawg
ChaoticDawg - avatar
0
ChaoticDawg , Thank you. I thought that only the first integer/variable in the equation would be incremented. Now it makes sense.
5th Mar 2021, 12:46 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
TahitišŸ· It will increment the value of the variable itself in memory. Note: The the precedence of the increment/decrement operator(s) to the addition/subtraction operator(s) may vary from one C++ compiler to next so the answer may also vary.
5th Mar 2021, 1:03 AM
ChaoticDawg
ChaoticDawg - avatar
0
Ok, ChaoticDawg . Now, could you explain what is a compiler? Thanks.
5th Mar 2021, 1:12 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar