+ 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; }
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
+ 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.
+ 5
Why do we have questions with undefined, compiler dependent answers in the quiz section. I can't specifically remember what questions though.
+ 2
I know about it
+ 2
I think int a2=12 is correct