简体   繁体   English

ANSI C 1999 TC3中的逗号运算符示例

[英]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. 当我试图找出结果时, !0的结果是实现定义,期望它不等于零,我只是读了一些令我困惑的东西。

(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) (顺便说一句,它可以在某些实现-11还是严格定义?如果我可以在评论中告诉别人,那会很好)

But my real question is: 但是我真正的问题是:

in

6.5.17 Comma operator 2 6.5.17逗号运算符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 . 由于给t分配了3 ,在下一个序列中它增加了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 !0等于1
  2. In (t=3, t+2) , there is a sequence point between the assignment and the access to t . (t=3, t+2) ,在赋值和对t的访问之间存在一个序列点。 The expression is defined, it evaluates to 5 and leaves the value 3 in t . 定义了表达式,计算结果为5 ,将值3t It would be undefined if there was no sequence point between the two, for instance, (t=3)+(t+2) . 如果两者之间没有序列点,例如(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. 该函数具有三个参数,第二个参数的值为5。

Emphasised the relevant portion to clarify your doubt. 强调相关部分以澄清您的疑问。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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