There is no correct answer.#include <stdio.h>
int main()
{
int a =4;
int result=++a + a--;
printf("%d",result);
}
RESULT: if i dry run and go from left to right then result is 8
or going from right to left result is 10.
Neither is the correct answer why so whats the logic
Order of operations require that this line parses as if it were:#include <stdio.h>
int main()
{
int a =4;
int result=++a + a--;
printf("%d",result);
}
RESULT: if i dry run and go from left to right then result is 8
or going from right to left result is 10.
Neither is the correct answer why so whats the logic
A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performedThe underlying problem is that the C language specification defines "sequence points" which are points during program execution at which all side-effects before it must have been applied and at which all side-effects after it must not have been applied.
i = ++i + i++; // undefined behavior
i = i++ + 1; // undefined behavior
f(++i, ++i); // undefined behavior
f(i = -1, i = -1); // undefined behavior
What assumptions are you making that result in (a--) evaluating to 3? This would require that at the time it was evaluated, the value stored in a was 3.result = (++a) + (a--);
This is a thought process and the results should never be relied on in any real engineering application.
Given a = 4 initially
(++a) = 5
(a--) = 3
but it really depends on how the compiler performs its operations.
Hence, in conclusion, the result is indeterminate.
There IS no sequence point within that line -- THAT's why the writer of the compiler has the option to apply the side effect in whatever order and at whatever time they choose.https://en.cppreference.com/w/c/language/operator_precedence#cite_note-1 acc to this link the unary operators have higher precedence than arithmetic addition so ++ and -- will operate first now a -- has higher precedence than ++a ...so a-- gets executed which gives 4 only, post decremnt a now is 3 and ++a gives 4 so result is 8 and in this line int result=++a + a--; where is the sequence point in this line
Associativity has to do with the order in which operations are applied. Evaluation has to do with evaluating an expression.what is the difference between associativity and evaluation?"
what is the meaning of this line in simple words-> {If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object } ??
by Duane Benson
by Aaron Carman
by Aaron Carman