To expand on tye: What gcc does for "i=i++;" is conforming behaviour for ISO C. What any implementation does for it is conforming!

This sort of code (technically, modifying a variable more than once between sequence points) is specifically said by the standard to evoke undefined behaviour (see the comp.lang.c FAQ). In the comp.lang.c.* newsfroups, it is said that the compiler could compile code which causes daemons to fly out of your nose (aka nasal daemons)! It could also choose to reformat your hard disks except on Friday the 13th, return the value 17, or do anything else.

Is this "broken"? Not really. By not specifying evaluation order more precisely, the standard allows the compiler a lot more latitude in compiling efficient code. This, at the price of disallowing some essentially useless code. (Would you be happier if the standard said "i=i++; is guaranteed to set the value of i to -29"? Would you use it?)

Note that all this applies to code generation, not to parsing. That "i++ - ++i + i" is parsed as "(i++ - ++i) + i" has nothing to do with the code emitted by the compiler. Parsing has no idea of "the same" object; that is part of the semantics of C, not its syntax. But not everything illegal is a syntax error!

Perl has not had such a rigourous standardization. Furthermore, only one implementation exists at any time (assuming we decide 5.004, 5.005, 5.00503, 5.6.0, 5.6.1, and the rest are all different). So it is very tempting to apply the "test and see" engineering technique.

I'd join tye in recommending you avoid this. First, because it isn't documented behaviour. Unfortunately, Perl has loads of behaviour that couldn't really be said to be "documented" and still gets used. The thing is, these other properties are actually useful. The syntax for them evokes a specific interpretation. Can you really give a good argument for compiling "(++A)+(B++)" (legal code, if A and B are different objects) as any of the following codes, rather than any of the others?

  1. LOAD R1, A INCR R1 LOAD R2, B ADD R3, R1, R2 INCR R2 STORE A, R1 STORE B, R2
  2. LOAD R1, A INCR R1 STORE A, R1 LOAD R2, B ADD R3, R1, R2 INCR R2 STORE B, R2
Note that if A and B happen to be the same object, you get different results...


In reply to Re: $i=$i++ by ariels
in thread $i=$i++ by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.