Question:
Someone please explain these outputs...?
2011-12-08 22:01:18 UTC
In c, if a=5, then
a++ + a++ + a++ + a++ yields 20 answer but,
a++ + a++ + ++a + a++ yields 24 answer. how?
Four answers:
Reddy
2011-12-08 22:07:15 UTC
1) We use the increment and decrement Operators in for and while loops extensively. while ++n and --n mean the same thing when they form statements independently, they behave differently when they are used in expressions.



2) The increment and decrement Operators can be utilized in 2 different ways, depending on whether the operator is written before or after the operand.





3) If the operator follows the operand (Ex :i++) then the value of the operand will be altered after it is utilized.

----------------------------



Ex: /* example of display then increment and increment and display*/



#include

void main()

{

int i=1;

int j=1;



printf("i=%d",i);

printf(" i++=%d",i++);

printf(" i=%d",i);



printf("\n-------------\n");

printf("j=%d",j);

printf(" ++j=%d",++j);

printf(" j=%d",j);

}

Output:



i=1 i++=1 i=2



-------------



j=1 ++j=2 j=2
Shadow Wolf
2011-12-09 06:36:28 UTC
Depending on the value of a and when it is incremented, it will give you different numbers.



So if we remove the increments it will become clearer.



The first becomes a = 5:

a + a + a + a

Because they are all post increment, a remains 5 until after the expression is evaluated.



The second becomes a = 6:

a + a + a + a

With the pre increment, a is incremented before it is evaluated.



After execution in both cases a will be 9



Every time I see one of these I have to question why some of the various examples have to be taught. If you are writing code that is clear and easy to understand, you won't do things like this. Pretty much no one does this anyway except for obfuscated code competitions. Perhaps it is a lesson in things that are legal within a programming language but you should never do.



If you want to really make things confusing, add decrement operators to the mix at different places. Then add parenthesis to further confuse matters and force even more pre and post increments to trigger in odd ways.



Sometimes the only way to really figure this stuff out is to compile it and examine the resulting assembly code using a compiler that will generate it.



Shadow Wolf
Barel166
2011-12-09 06:46:43 UTC
Hi there,



This is a Prefix vs Postfix problem.



Taking your example of a = 5:

a++ tells the compiler to add 1 to the value within "a" AFTER the line is fully processed.

++a tells the compiler to add 1 to the value within "a" BEFORE the line is fully processed.



If you printf a++, you should get a result of 5.

If you printf ++a, you should get a result of 6.



a++ + a++ + a++ + a++; is all one expression to process in the same line. Since "a" isn't increased until after the the line is processed, then:



(a++ = 5) + (a++ = 5) + (a++ = 5) + (a++ = 5) = 20



Now, in your second expression, since you introduce a "++a" this tells the compiler: first add 1 to "a" and THEN evaluate the entire expression, so this turns out to be:



(a++ = 6) + (a++ = 6) + (++a = 6) + (a++ = 6) = 24.



The compiler basically looks at ++a first and performs that operation and then it looks at the rest of the line and basically does the same thing it did in the previous expression (doesnt change "a" until the line is processed). But since ++a changed it to 6, then 6 is stored in "a".
?
2011-12-09 17:46:32 UTC
both lines have undefined behaviour

you are modifying a more than once between sequence points

that is a no no.

look here:

http://c-faq.com/expr/seqpoints.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...