gpt4 book ai didi

c - C语言中if(a,b,c,d)是什么意思

转载 作者:太空宇宙 更新时间:2023-11-04 05:04:31 25 4
gpt4 key购买 nike

Possible Duplicate:
C++ Comma Operator

我看过C语言的if语句。像这样。

if (a, b, c, d) {

blablabla..
blablabla..

}

这个if语句是什么意思?

最佳答案

您所拥有的是逗号运算符的示例。它计算所有四个表达式,但对 if 语句使用 d

除非 d 以外的表达式有副作用(例如 a++)它们是无用的。您可以通过小程序看到它的运行情况:

#include <stdio.h>
int main (void) {
if (1,0) printf ("1,0\n");
if (0,1) printf ("0,1\n");
return 0;
}

哪些输出:

0,1

大多数人在没有意识到的情况下使用它,例如:

for (i = 0, j = 100; i < 10; i++, j--) ...

i = 0j = 100i++j++ 是两个完整表达式的组成部分其中使用逗号运算符。

标准的相关部分是C11 6.5.17 Comma operator:

Syntax:

expression:
   assignment-expression
   expression , assignment-expression

Semantics:

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

EXAMPLE:

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.

关于c - C语言中if(a,b,c,d)是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10511325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com