Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Under my compiler, the code int i = 7; printf(“%d “, i++ * i++); prints 49. Regardless of the order of evaluation, shouldn it print 56?

1
Posted

Under my compiler, the code int i = 7; printf(“%d
“, i++ * i++); prints 49. Regardless of the order of evaluation, shouldn it print 56?

0

The operations implied by the postincrement and postdecrement operators ++ and — are performed at some time after the operand’s former values are yielded and before the end of the expression, but not necessarily immediately after, or before other parts of the expression are evaluated.

0

Although the postincrement and postdecrement operators ++ and — perform the operations after yielding the former value, the implication of “after” is often misunderstood. It is _not_ guaranteed that the operation is performed immediately after giving up the previous value and before any other part of the expression is evaluated. It is merely guaranteed that the update will be performed sometime before the expression is considered “finished” (before the next “sequence point,” in ANSI C’s terminology). In the example, the compiler chose to multiply the previous value by itself and to perform both increments afterwards. The behavior of code which contains multiple, ambiguous side effects has always been undefined. Don’t even try to find out how your compiler implements such things (contrary to the ill- advised exercises in many C textbooks); as K&R wisely point out, “if you don’t know _how_ they are done on various machines, that innocence may help to protect you.” References: K&R I Sec.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.