Question

When I was trying to figure out, !0's result is implementation defined expecting that it shall be unequal to zero I just read something what confused me.

(By the way may it be on some implementations -1 or 1 or is it strict defined? If any one could me tell in comment would be nice)

But my real question is:

in

6.5.17 Comma operator 2

is said:

If an attempt is made to modify the result of a comma operator or to access it after the next sequence point, the behavior is undefined.

In exactly the next line there is an example how to parse a parameter into a function with use of comma operator.

f(a, (t=3, t+2), c);

But the example is in point of my knowledge so far undefined behavior, isn't it? Since t gets assigned 3 and in the next sequence it gets increased by 2.

But the standard doesn't mention that the example isn't valid.

Or is an assignment to be not understood as modification?

Was it helpful?

Solution

  1. !0 evaluates to 1
  2. In (t=3, t+2), there is a sequence point between the assignment and the access to t. The expression is defined, it evaluates to 5 and leaves the value 3 in t. It would be undefined if there was no sequence point between the two, for instance, (t=3)+(t+2).

OTHER TIPS

I'm not sure what prompted you to ask the question. The section of the standard that you've picked up the example from clearly says:

As indicated by the syntax, the comma operator (as described in this subclause) cannot appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists of initializers). On the other hand, it can be used within a parenthesized expression or within the second expression of a conditional operator in such contexts. In the function call

        f(a, (t=3, t+2), c)

the function has three arguments, the second of which has the value 5.

Emphasised the relevant portion to clarify your doubt.

The comma operator introduces a sequence point, so that behavior should be well defined (first t is set to 3, then 2 is added to t yielding the result 5 but keeping 3 in t).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top