+ 3

What is the output of this code and how? Can anyone explain in detail as I am not getting it?

I am not getting why the post incremental and post decrement not working? https://code.sololearn.com/c9ralQeShIh4/?ref=app

15th Jun 2019, 10:57 AM
Vijay Sapkal
Vijay Sapkal - avatar
9 Respuestas
15th Jun 2019, 1:17 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 19
Anna , ~ swim ~ Thank you!😊 If you use these two expressions as statements by themselves, as in: a++; or ++a; you won’t observe any difference. The difference between pre-increment (++a) and the post-increment (a++) shows up only when you use the value of the expression. In both of the above statements, the increment of 'a' occurs, but the value of the expression is thrown away, and is not stored or printed or passed anywhere. But if you capture and use the value of the expression, you will observe the difference in behavior.
15th Jun 2019, 2:12 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 17
Precedence and associativity of postfix ++ and prefix ++ are different;  ➝ Precedence of postfix ++ is more than prefix ++, their associativity is also different. Associativity of postfix ++ is left to right   and associativity of prefix ++ is right to left. • https://code.sololearn.com/cdiP8gSPdSTi/?ref=apphttps://code.sololearn.com/c40cT81cpKl2/?ref=app
15th Jun 2019, 11:58 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 17
For example, int a = 42; int b; b = a++; // post-increment, // b will contain 42 Now, consider the following: int a = 42; int b; b = ++a; // pre-increment, // b will contain 43
15th Jun 2019, 2:18 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 16
Denise Roßberg I really would like someone to correct me... An easy way to remember how these operators behave involves these two rules: • If the operator appears 'before' the operand, the operation will occur 'before' the expression has been evaluated. • If the operator appears 'after' the operand, the operation will occur 'after' the expression has already been evaluated. While the actual sequence of operations at the instruction level can vary, this set of rules describes the effective behavior from a C source code point of view. https://code.sololearn.com/cGHSlT0PHyb8/?ref=app
15th Jun 2019, 1:43 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
Danijel Ivanović Is this correct? a = 3, b = 4 a++ + a-- use value of a, increment it (a is now 4) then use new value of a and decrement a (a is now 3) b = 3 + 4 = 7 a = 3 But I am not really unterstand how it comes to 15 in the second example. I think a = 7 + 8 but I am not sure.
15th Jun 2019, 12:24 PM
Denise Roßberg
Denise Roßberg - avatar
+ 6
I'll just drop my favorite link here: https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior The answer is undefined behavior. Never change the value of a variable more than once in one statement. All of these are wrong: i = ++i; i += ++i; x = i++ + ++i; printf("%d %d", i, ++i); etc.
15th Jun 2019, 1:24 PM
Anna
Anna - avatar
+ 2
Thanks, but can you tell me in simple words ?
15th Jun 2019, 12:23 PM
Vijay Sapkal
Vijay Sapkal - avatar
+ 1
I am also getting different answer after running code.Denise Roßberg
15th Jun 2019, 12:27 PM
Vijay Sapkal
Vijay Sapkal - avatar