This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: warning: operation on 'zero' may be undefined


Paul Schlie wrote:

For my own edification, it's not clear to me that the instruction sequence:

  int zero = 125;
  zero = (++zero)%3;

is ambiguous in any way, as I was under the impression that the left hand
side of an assignment expression will be assigned the resulting value of the
evaluation of it's right hand side, which unambiguously logically equivalent
to:

int zero = 125;
zero = ((zero = zero + 1) % 3) ;


It's a matter of language definition, there simply is a requirement in the C definition
that the increment of zero take place before or after the assignment. You are missing
a required sequence point. Basically the rule in C is that if you have an expression
(remember that an assignment is just an expression), and somewhere in that expression,
you use an increment operator on a variable, you can't mention that variable elsewhere
in the expression. End of story.


Yes, as you analyze, it is likely the compiler will behave as you expect, but it is
right to warn you that you have undefined code and you have no right to
expect that behavior.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]