逗号运算符 , 的作用是什么?
,
运算符在 C 中起什么作用?
What does the ,
operator do in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
,
运算符在 C 中起什么作用?
What does the ,
operator do in C?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
另一种用途(对已经提到的用途的补充)是一种避免单行 if 中出现大括号的方法。
Another use - complementary to the already mentioned - is a method to avoid braces in a single line if.
我恢复这个只是为了解决@Rajesh 和@JeffMercado 的问题,我认为这些问题非常重要,因为这是搜索引擎点击率最高的搜索之一。
以下面的代码片段为例,
它将打印
The
i
case is returned as所述的大多数答案。 所有表达式都按从左到右的顺序进行计算,但只有最后一个被分配给i
。(
表达式 )的结果是
1`。j
情况遵循不同的优先级规则,因为,
具有最低的运算符优先级。 由于这些规则,编译器会看到赋值表达式、常量、常量...。 表达式再次按从左到右的顺序进行计算,并且其副作用仍然可见,因此,由于j = 5<,
j
为5
/代码>。有趣的是,语言规范不允许
int j = 5,4,3,2,1;
。 初始化程序需要一个赋值表达式,因此不允许直接使用,
运算符。希望这可以帮助。
I'm reviving this simply to address questions from @Rajesh and @JeffMercado which i think are very important since this is one of the top search engine hits.
Take the following snippet of code for example
It will print
The
i
case is handled as explained by most answers. All expressions are evaluated in left-to-right order but only the last one is assigned toi
. The result of the(
expression )is
1`.The
j
case follows different precedence rules since,
has the lowest operator precedence. Because of those rules, the compiler sees assignment-expression, constant, constant .... The expressions are again evaluated in left-to-right order and their side-effects stay visible, therefore,j
is5
as a result ofj = 5
.Interstingly,
int j = 5,4,3,2,1;
is not allowed by the language spec. An initializer expects an assignment-expression so a direct,
operator is not allowed.Hope this helps.
我发现它唯一有用的地方是当您编写一个时髦的循环时,您想要在其中一个表达式(可能是 init 表达式或循环表达式)中执行多项操作。例如:
如果有任何语法错误,请原谅我或如果我混合了任何不严格的 C 语言。我并不是说 , 运算符是好的形式,但这就是你可以使用它的目的。在上面的情况下,我可能会使用
while
。循环,这样 init 和循环上的多个表达式会更明显(并且我会内联初始化 i1 和 i2 而不是声明然后初始化......等等等等。)The only place I've seen it being useful is when you write a funky loop where you want to do multiple things in one of the expressions (probably the init expression or loop expression. Something like:
Pardon me if there are any syntax errors or if I mixed in anything that's not strict C. I'm not arguing that the , operator is good form, but that's what you could use it for. In the case above I'd probably use a
while
loop instead so the multiple expressions on init and loop would be more obvious. (And I'd initialize i1 and i2 inline instead of declaring and then initializing.... blah blah blah.)它会导致对多个语句进行评估,但仅使用最后一个语句作为结果值(我认为是右值)。
所以...
应该导致 x 设置为 8。
It causes the evaluation of multiple statements, but uses only the last one as a resulting value (rvalue, I think).
So...
should result in x being set to 8.
正如前面的答案所述,它会评估所有语句,但使用最后一个语句作为表达式的值。 就我个人而言,我只发现它在循环表达式中有用:
As earlier answers have stated it evaluates all statements but uses the last one as the value of the expression. Personally I've only found it useful in loop expressions:
逗号运算符将其两侧的两个表达式合并为一个,并按从左到右的顺序对它们进行求值。 右侧的值作为整个表达式的值返回。
(expr1, expr2)
就像{ expr1; 表达式2; }
但您可以在函数调用或赋值中使用expr2
的结果。它经常出现在
for
循环中来初始化或维护多个变量,如下所示:除此之外,我只在另一种情况下“愤怒地”使用它,当结束两个应该始终执行的操作时在宏中一起走。 我们的代码将各种二进制值复制到字节缓冲区中以便在网络上发送,并在我们到达的位置维护一个指针:
值是
short
或int
我们这样做了:后来我们了解到这不是真正有效的 C,因为
(short *)ptr
不再是左值并且不能递增,尽管我们当时的编译器不介意。 为了解决这个问题,我们将表达式分成两部分:但是,这种方法依赖于所有开发人员记住始终将这两个语句放入其中。 我们想要一个函数,您可以在其中传递输出指针、值和值的类型。 这是 C,而不是带有模板的 C++,我们不能让函数采用任意类型,因此我们选择了一个宏:
通过使用逗号运算符,我们可以按照我们的意愿在表达式或语句中使用它:
I'我并不是说这些例子都是好的风格! 事实上,我似乎记得 Steve McConnell 的 Code Complete 建议不要在
for
循环中使用逗号运算符:为了可读性和可维护性,循环应该仅由一个变量控制,并且for
行本身中的表达式应该只包含循环控制代码,而不是其他额外的初始化或循环维护位。The comma operator combines the two expressions either side of it into one, evaluating them both in left-to-right order. The value of the right-hand side is returned as the value of the whole expression.
(expr1, expr2)
is like{ expr1; expr2; }
but you can use the result ofexpr2
in a function call or assignment.It is often seen in
for
loops to initialise or maintain multiple variables like this:Apart from this, I've only used it "in anger" in one other case, when wrapping up two operations that should always go together in a macro. We had code that copied various binary values into a byte buffer for sending on a network, and a pointer maintained where we had got up to:
Where the values were
short
s orint
s we did this:Later we read that this was not really valid C, because
(short *)ptr
is no longer an l-value and can't be incremented, although our compiler at the time didn't mind. To fix this, we split the expression in two:However, this approach relied on all developers remembering to put both statements in all the time. We wanted a function where you could pass in the output pointer, the value and and the value's type. This being C, not C++ with templates, we couldn't have a function take an arbitrary type, so we settled on a macro:
By using the comma operator we were able to use this in expressions or as statements as we wished:
I'm not suggesting any of these examples are good style! Indeed, I seem to remember Steve McConnell's Code Complete advising against even using comma operators in a
for
loop: for readability and maintainability, the loop should be controlled by only one variable, and the expressions in thefor
line itself should only contain loop-control code, not other extra bits of initialisation or loop maintenance.逗号运算符将计算左侧操作数,丢弃结果,然后计算右侧操作数,这将成为结果。 链接中指出的惯用用法是在初始化
for
循环中使用的变量时使用,它给出了以下示例:否则没有很多伟大< /em> 使用逗号运算符,尽管很容易被滥用而生成难以阅读和维护的代码。
从 C99 标准草案 语法如下如下:
第 2 段说:
脚注 97 说:
这意味着您不能分配给逗号运算符的结果。
需要注意的是,逗号运算符具有 最低优先级,因此有使用
()
可以产生很大差异的情况,例如:将有以下输出:
The comma operator will evaluate the left operand, discard the result and then evaluate the right operand and that will be the result. The idiomatic use as noted in the link is when initializing the variables used in a
for
loop, and it gives the following example:Otherwise there are not many great uses of the comma operator, although it is easy to abuse to generate code that is hard to read and maintain.
From the draft C99 standard the grammar is as follows:
and paragraph 2 says:
Footnote 97 says:
which means you can not assign to the result of the comma operator.
It is important to note that the comma operator has the lowest precedence and therefore there are cases where using
()
can make a big difference, for example:will have the following output:
表达式:
首先计算
表达式1
,然后计算表达式2
,最后返回整个表达式的表达式2
的值。The expression:
First
expression1
is evaluated, thenexpression2
is evaluated, and the value ofexpression2
is returned for the whole expression.我见过在 while 循环中使用最多的:
它将执行操作,然后根据副作用进行测试。 另一种方法是这样做:
I've seen used most in
while
loops:It will do the operation, then do a test based on a side-effect. The other way would be to do it like this: