b=a*++a & b=a*a++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

b=a*++a & b=a*a++

if a=2 b=a*++a --> b=9 I get how this one return 9 for b but i dont understand why answer of b=a*a++ is 6??

13th Apr 2019, 7:17 AM
Hesam Salehi
Hesam Salehi - avatar
7 Answers
13th Apr 2019, 8:07 AM
Shadow Ninja[#Inactive]
Shadow Ninja[#Inactive] - avatar
+ 3
Hmmm... seems like c++ handles it in a different way. I just tried this with JS and it gave me 6 and 4. It seems C++ evaluate expressions from the right instead from left like in js. So let me post a Question that shows you the difference between x++ (postfix) and --x (prefix) increment. https://www.sololearn.com/discuss/1030267/?ref=app https://www.sololearn.com/discuss/160327/?ref=app https://www.sololearn.com/discuss/266300/?ref=app https://www.sololearn.com/discuss/755725/?ref=app So with c++ case, in the first problem it will parse to this. b = a*++a <---- evaluate from here = 3*3 = 9 In the second case x get incremented after that statement. b = a*a++ = 3*2 = 6
13th Apr 2019, 7:40 AM
Seniru
Seniru - avatar
+ 2
Have you missed something? It seems like both answers are incorrect 😞
13th Apr 2019, 7:28 AM
Seniru
Seniru - avatar
0
#include <iostream> using namespace std; main() { int a=2; int b; b=a*++a; cout<<b; } #include <iostream> using namespace std; main() { int a=2; int b; b=a*a++; cout<<b; }
13th Apr 2019, 7:32 AM
Hesam Salehi
Hesam Salehi - avatar
0
hesam salehi Imagine you want to create your own C/C++ compiler. If there's a statement like this: int a = 25 + 8 * 3; , the value of a has to be 49, because multiplication is performed before addition (in other words, * has a higher precedence than +). If your compiler doesn't care about operator precedence and just evaluates everything from left to right, you'll get the result (25+8)*3 = 99 which is obviously wrong. So there's a severe bug in your compiler and it doesn't comply with the C standard. Now look at the expression int i = 2; i *= ++i; That's the same as if I were to say to you "please calculate 2 * 2, and while you're at it, replace 2 with 3". Your result will completely depend on how you understand the assignment. You might get any of the results 4 (because you calculate 2*2 first and then replace i with 3), 6 (because you increment 2 midway and calculate 2*3) or even 9 (because you increment 2 first and then calculate 3*3). All of these results are correct because I didn't specify in which order you are supposed to solve the problem. I leave it up to you when in the process you perform the increment. It's the same with C. The C standard doesn't specify the order in which an expression like i *= ++i has to be performed. There are several correct ways to do it and it's up to the compiler how to do it. You might get three different results with three different compilers. That's why you must never change the value of a variable more than once within the same expression and avoid expressions like a += ++a or b = ++a * a++ etc. at all cost. Even if you get the expected result on your platform and with your compiler, the result is actually undefined.
13th Apr 2019, 9:22 AM
Anna
Anna - avatar
0
hesam salehi Imagine you want to create your own C/C++ compiler. If there's a statement like this: int a = 25 + 8 * 3; , the value of a has to be 49, because multiplication is performed before addition (in other words, * has a higher precedence than +). If your compiler doesn't care about operator precedence and just evaluates everything from left to right, you'll get the result (25+8)*3 = 99 which is obviously wrong. So there's a severe bug in your compiler and it doesn't comply with the C standard. Now look at the expression int i = 2; i *= ++i; That's the same as if I were to say to you "please calculate 2 * 2, and while you're at it, replace 2 with 3". Your result will completely depend on how you understand the assignment. You might get any of the results 4 (because you calculate 2*2 first and then replace i with 3), 6 (because you increment 2 midway and calculate 2*3) or even 9 (because you increment 2 first and then calculate 3*3). All of these results are correct because I didn't specify in which order you are supposed to solve the problem. I leave it up to you when in the process you perform the increment. It's the same with C. The C standard doesn't specify the order in which an expression like i *= ++i has to be evaluated. There are several formally correct ways to do it and it's up to the compiler how to do it. You might get three different results with three different compilers. That's why you must never change the value of a variable more than once within the same expression and avoid expressions like a += ++a or b = ++a * a++ etc. at all cost. Even if you get the expected result on your platform and with your compiler, the result is actually undefined/implementation dependent.
13th Apr 2019, 9:33 AM
Anna
Anna - avatar