+ 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
9 Respuestas
+ 19
Denise Roßberg
I'm trying to find best explanation, also I have doubts ...!!?
• https://code.sololearn.com/cZh21tT8Yn4D/?ref=app
• https://code.sololearn.com/ck689G7hHxg9/?ref=app
Vijay Sitaram Sapkal
Check out Anna's answer on this post -> https://www.sololearn.com/Discuss/1838592/?ref=app
+ 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.
+ 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=app
• https://code.sololearn.com/c40cT81cpKl2/?ref=app
+ 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
+ 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
+ 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.
+ 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.
+ 2
Thanks, but can you tell me in simple words ?
+ 1
I am also getting different answer after running code.Denise Roßberg