简体   繁体   中英

The comma operator example in ansi c 1999 TC3

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?

  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) .

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.

逗号运算符引入了一个序列点,因此应明确定义行为(首先将t设置为3 ,然后将2加到t得到结果5但在t保留3 )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM