+ 2

Why? 😁

#include <iostream> using namespace std; int main() { int a1 = 5; ++a1; cout << "a1 = " << a1 << endl;//output 6 ++a1; cout << "a1 = " << a1 << endl;//output 7 int a2 = 5; a2 = ++a2 + ++a2; cout << "a2 = "<< a2 << endl;//output 14 return 0; }

17th Mar 2019, 8:05 AM
Andrey Stepanov
Andrey Stepanov - avatar
6 ответов
+ 13
Andrey Stepanov this type of increment operation is totally dependent on how compiler read it as for output 14 it works like this way As a2 is initially 5 Pre increment will convert it to 6 And again pre increment will convert it to 7 So last evaluated value of a2 is 7 which is assigned to both a2 And sum this valued results in 14 firstly a2 = 6 + 7 //but last evaluated value is 7 so a2 previous value is too convert in 7 a2 =7 +7 =14
17th Mar 2019, 8:11 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 10
Mostly what I get this in three variables it's work so surprising for first time like this way. int x=3; y = ++x * ++x * ++x; Now some compiler treat it as (++x * ++x) * ++x so by this the output will be (4 * 5)*6 and due to in same expression in brackets the 4 convert in 5 and it gives undefined behaviour and result as (5*5)*6 =150 Some compiler will give this like 6*6*6 = 216 which is described in my first comment Some compiler evaluate it's as 4*(6*6) = 144 so it's totally on compiler how it looks over the program.
17th Mar 2019, 8:36 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
Why do we have questions with undefined, compiler dependent answers in the quiz section. I can't specifically remember what questions though.
23rd Mar 2019, 2:20 AM
Sonic
Sonic - avatar
+ 2
I know about it
29th Mar 2019, 3:06 PM
Jeetu Kumar
Jeetu Kumar - avatar
+ 2
I think int a2=12 is correct
29th Mar 2019, 3:07 PM
Jeetu Kumar
Jeetu Kumar - avatar