#include <iostream> using namespace std; int main() { int a=0,u=0; u=(a) * ++a; cout<<u; } | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 3

#include <iostream> using namespace std; int main() { int a=0,u=0; u=(a) * ++a; cout<<u; }

ans is 1 can someone explain

12th Sep 2018, 5:50 PM
Arun
12 Respuestas
+ 4
You don't post the question with your answer than you have write. https://www.sololearn.com/discuss/1316935/?ref=app
12th Sep 2018, 5:56 PM
program
program - avatar
+ 3
Mark We called the increment and decrement. a += 1 has remplaced by a++ but ++a can't change the number because the ++ is the left of a.
12th Sep 2018, 6:41 PM
program
program - avatar
+ 3
Here is  what the C standard says: C11: 6.5 Expressions: If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84).
12th Sep 2018, 8:33 PM
Mark
+ 2
mark u get 1 only after interchanging..
12th Sep 2018, 6:29 PM
Arun
+ 2
by the way my doubt is that parameters should be solved first so it become u=(0)*1
12th Sep 2018, 6:39 PM
Arun
+ 2
0 on gcc x64
25th Sep 2018, 10:39 PM
iJustErik gah
iJustErik gah - avatar
+ 1
this is undefined behaviour. some compilers read the arguments from left to right, and others right to left.
12th Sep 2018, 6:15 PM
Bebida Roja
Bebida Roja - avatar
+ 1
ooppps
12th Sep 2018, 6:29 PM
Arun
+ 1
Hi guys, So, I checked everything again and tested in other compilers, and I notice that different compilers will give different results. For example, clang will give the warning -Wunsequenced, saying unsequenced modification and access to "a". Thus, in the end this is an undefined behaviour, as Bebida Roja mentioned.
12th Sep 2018, 8:26 PM
Mark
0
i think it because of you make an ++a.... so the compiler will do the a+1 before the u=(a)*++a.... so it would be 1×1
16th Sep 2018, 11:36 PM
Marshelino Pontoh
Marshelino Pontoh - avatar
0
answer is 1
22nd Sep 2018, 4:01 PM
Sapna Rao
Sapna Rao - avatar
0
++a matlab change then use so a=1. now , a=1 so answer is 1*1=1
23rd Sep 2018, 9:46 AM
Pavitra Rathore
Pavitra Rathore - avatar