+ 1
Int a=2, b=4, c; c=a+++++b/2+a℅2;
12 odpowiedzi
+ 2
You forgot to write what you need help with? It saves time...
You could put this in a code bit in the code playground and you'd see the answer.
https://sololearn.com/compiler-playground/Wek0V1MyIR2r/?ref=app
https://sololearn.com/compiler-playground/W0RD0yyfZT6p/?ref=app
https://sololearn.com/compiler-playground/W3uiji9X28C1/?ref=app
https://sololearn.com/compiler-playground/W0uW3Wks8UBk/?ref=app
+ 2
smitarani subudhi Your obfuscated code runs into a problem with your string of plus signs.
According to C and C++ standards, your code would have "undefined behaviour" due to multiple operators with side effects (++) acting on same variable within the expression.
There is also the problem that your 5 plus signs can be interpreted in a variety of configurations of x++, ++x, and x+y operators leading to different results.
One possible interpretation is
c = ((a++)++)+(b/2)+(a%2);
a = 4, b = 4, c = 2+2+0 = 4
Another interpretation might be
c = a+(++(++(b/2)))+(a%2);
a = 2, b = 4, c = 2+4+0 = 6
Yet another interpretation could be
c = a+((++(++b))/2)+(a%2);
a = 2, b = 6, c = 2+3+0 = 5
+ 1
Solo Based off operator precedence, the first interpretation I listed is what most compilers would use.
Since x++ operator has higher precedence than ++x or x+y.
So without parenthesis to make clear,
The compiler would try post-fix increment twice on a (since x++ operator evaluates left-to-right),
and be left with single plus operator.
Post-fix increments would not increment a until after c is evaluated.
Hence evaluation as
c = ((a++)++) + (b/2) + (a%2)
c = 2 + (4/2) + (2%2) = 2 + 2 + 0 = 4
a = 2 + 1 + 1 = 4
+ 1
According to python correct answer 4
0
c = a++;
c = c + ++b/2 + a%2;
Or
c = a++;
c += ++b/2 + a%2;
0
Solo In neither case would c start off as only a++.
Assignment operator has one of lowest operator precedence.
So value of c would not be evaluated until the very end.
0
Shardis Wolfe, you misunderstood me.
I did not demonstrate the order of execution of this expression, I showed a ready-made solution to this problem without warning about an undefinite expression...😎
0
Shardis Wolfe, by the way, your interpretations are not correct.
0
Shardis Wolfe, tell me, where did you get this opinion about the work of the compiler?
If you are right, then it is logical to assume that your expression
c = ((a++)++) + (b/2) + (a%2);
the compiler will read it without errors.
I doubt that's the case.
Have you checked it?-)
0
Solo
c=((a++)++) + (b/2) + (a%2);
is how compilers would apply the operator orders of precedence to
c=a+++++b/2+a%2;
Since x++ has higher precedence than ++x, compiler would keep trying to apply it.
So 1st pass - c=(a++)+++b/2+a%2
2nd pass - c=((a++)++)+b/2+a%2
3rd pass - c=((a++)++)+(b/2)+a%2
4th pass - c=((a++)++)+(b/2)+(a%2)
The reason it would evaluate as undefined behaviour instead of a consistent answer is not all compilers may guarantee that the side effect of the a++ increment will be evaluated after c is evaluated.
0
Shardis Wolfe, are you a bot?-)
0
Nope. Just someone who had too much free time last night 😔