GeekInterview.com
Series: Subject: Topic:
Question: 37 of 587

decrement operator in C

int a=3,y; y=++a + a+++ --a+ ++a; printf("%d%d",y,a); Why value of y become 16.
Asked by: Anshu Tiwari | Member Since Oct-2011 | Asked on: Oct 18th, 2011

View all questions by Anshu Tiwari

Showing Answers 1 - 3 of 3 Answers
jbode

Answered On : Oct 18th, 2011

View all answers by jbode

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.

  
Login to rate this answer.
kala725

Answered On : Dec 11th, 2011

View all answers by kala725

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.

  
Login to rate this answer.
yashptl31

Answered On : Jan 4th, 2012

View all answers by yashptl31

first check then say answer is 15... its 16..

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.