Can anyone explain the output of this?
Code: Select all
#include<stdio.h>
main()
{
int a,i;
i=5;
a=i++ + ++i;
printf("\na=%d i=%d",a,i);
i=5;
i=a=i++ + ++i;
printf("\na=%d i=%d",a,i);
}
Code: Select all
a=12 i=7
a=12 i=13
Code: Select all
#include<stdio.h>
main()
{
int a,i;
i=5;
a=i++ + ++i;
printf("\na=%d i=%d",a,i);
i=5;
i=a=i++ + ++i;
printf("\na=%d i=%d",a,i);
}
Code: Select all
a=12 i=7
a=12 i=13
Use i * 1 + 1 and ++i instead.mystran wrote:
Even "i + ++i" is equally bad, and totally undefined as is "i = ++i".
If you are forced to make such a excercise, asnwer "the program ordered pizza with pepperony and onions" and quote the above. As for the referred question 11.33:[...]
The behavior of code which contains multiple, ambiguous side effects has always been undefined. (Loosely speaking, by ``multiple, ambiguous side effects'' we mean any combination of ++, --, =, +=, -=, etc. in a single expression which causes the same object either to be modified twice or modified and then inspected. This is a rough definition; see question 3.8 for a precise one, and question 11.33 for the meaning of ``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&R1 Sec. 2.12 p. 50
K&R2 Sec. 2.12 p. 54
ANSI Sec. 3.3
ISO Sec. 6.3
CT&P Sec. 3.7 p. 47
PCS Sec. 9.5 pp. 120-1
Emphasis mine.Briefly: implementation-defined means that an implementation must choose some behavior and document it. Unspecified means that an implementation should choose some behavior, but need not document it. Undefined means that absolutely anything might happen. In no case does the Standard impose requirements; in the first two cases it occasionally suggests (and may require a choice from among) a small set of likely behaviors.
In C, yes (AFAICS). In C++, no.mystran wrote: Excuse me, Solar, but whether one uses pre or post operators when the value isn't used is a matter of choice.
Considering the death star size, you don't want to copy that without reason... On the other hand, I'd choose *star for destructing a planet and ++star for going to the next one. Still, you'd have to return a DeathStar at the old planet when doing a star++, as opposed to a DeathStar at the new planet (where you are) with ++star.Solar wrote: In C, yes (AFAICS). In C++, no.
In C++, the operator '++' can be overloaded to do whatever the programmer considered intuitive for a '++' to do for the class in question, which can be a very non-trivial operation on a quite complex data structure. (Like, "annihilate next planet on list" for an object of the DeathStar class. )
Now, prefix '++' takes the next planet from the list, annihilates it, and returns a reference to 'this' (the DeathStar). Easy enough.
But postfix '++' takes the next planet from the list, annihilates it, and must return a reference to a DeathStar that has not yet annihilated that planet.
There's a copy constructor you don't want to call out of carelessness. And that's why you should grow into the habit of, all other things equal, using prefix '++' and '--'. You never know when you'll have to handle that DeathStar class.
back to the origin question... GCC probably does:Neo wrote: I'm having trouble understanding how this works.
Can anyone explain the output of this?The output isCode: Select all
#include<stdio.h> main() { int a,i; i=5; a=i++ + ++i; printf("\na=%d i=%d",a,i); i=5; i=a=i++ + ++i; printf("\na=%d i=%d",a,i); }
Code: Select all
a=12 i=7 a=12 i=13
Code: Select all
- Preincrements
- The expression(s) and assignment(s)
- Postincrements
C99, 6.5 Expressions, paragraph 2 f.:if you have marked then unary operator's precendence in rigth to left means in i++ + ++i second ++i will be computed first followed by i++
Boldface is mine.Between the previous and the next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
The grouping of operators and operands is indicated by the syntax. Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.