Decrement operator in C

Int a=3,y;
y=++a + a+++ --a+ ++a;
printf("%d%d",y,a);
Why value of y become 16.

Questions by Anshu Tiwari

Showing Answers 1 - 9 of 9 Answers

From the C language standard, section 6.5:
-----------------------
2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.72) Furthermore, the prior value shall be read only to determine the value to be stored.73).
...
72) A floating-point status flag is not an object and can be set more than once within an expression.

73) This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;
while allowing
i = i + 1;
a[i] = i;
--------------------------

The expression y=++a + a+++ --a+ ++a violates this restriction; a is being modified multiple times by the evaluation of the expressions ++a, a++, and --a, thus the behavior is *undefined*.

C does not guarantee that operands are evaluated from left to right (except for the &&, ||, ?:, comma, and function-call () operators), nor does it guarantee that the side effects from the ++ and -- operators are applied immediately after each subexpression has been evaluated. Thus, the actual result will vary depending on the compiler, optimization settings, surrounding code, etc.

Since the behavior is undefined, *any* result (including 16) is allowed.

  Was this answer useful?  Yes

kala725

  • Dec 11th, 2011
 

Answer will Be 15 Not 16

++a means a=4
a++ means for the current evaluation a=4 and for next a=5
--a means a=3
and at last ++a means a=4

so p=4+4+3+4=15
and a=5 as Post ++ after the last value of A.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions