C 逗号运算符的使用
你看到它在 for 循环语句中使用,但它在任何地方都是合法的语法.如果有的话,您在其他地方发现了它的哪些用途?
You see it used in for loop statements, but it's legal syntax anywhere. What uses have you found for it elsewhere, if any?
推荐答案
C 语言(以及 C++)在历史上是两种完全不同的编程风格的混合,可以将其称为语句编程"和表达式编程"".如您所知,每种过程式编程语言通常都支持诸如排序和分支之类的基本结构(请参阅结构化编程).这些基本结构以两种形式出现在 C/C++ 语言中:一种用于语句编程,另一种用于表达式编程.
C language (as well as C++) is historically a mix of two completely different programming styles, which one can refer to as "statement programming" and "expression programming". As you know, every procedural programming language normally supports such fundamental constructs as sequencing and branching (see Structured Programming). These fundamental constructs are present in C/C++ languages in two forms: one for statement programming, another for expression programming.
例如,当您根据语句编写程序时,您可能会使用由 ;
分隔的语句序列.当你想做一些分支时,你使用 if
语句.您还可以使用循环和其他类型的控制转移语句.
For example, when you write your program in terms of statements, you might use a sequence of statements separated by ;
. When you want to do some branching, you use if
statements. You can also use cycles and other kinds of control transfer statements.
在表达式编程中,您也可以使用相同的构造.这实际上是 ,
运算符发挥作用的地方.运算符 ,
只不过是 C 中顺序表达式的分隔符,即运算符 ,
在表达式编程中的作用与 ;
在语句中的作用相同编程.表达式编程中的分支是通过 ?:
运算符完成的,或者通过 &&
和 ||
运算符的短路评估属性完成.(不过,表达式编程没有循环.要用递归替换它们,您必须应用语句编程.)
In expression programming the same constructs are available to you as well. This is actually where ,
operator comes into play. Operator ,
in nothing else than a separator of sequential expressions in C, i.e. operator ,
in expression programming serves the same role as ;
does in statement programming. Branching in expression programming is done through ?:
operator and, alternatively, through short-circuit evaluation properties of &&
and ||
operators. (Expression programming has no cycles though. And to replace them with recursion you'd have to apply statement programming.)
例如下面的代码
a = rand();
++a;
b = rand();
c = a + b / 2;
if (a < c - 5)
d = a;
else
d = b;
这是传统语句编程的一个例子,在表达式编程方面可以重写为
which is an example of traditional statement programming, can be re-written in terms of expression programming as
a = rand(), ++a, b = rand(), c = a + b / 2, a < c - 5 ? d = a : d = b;
或作为
a = rand(), ++a, b = rand(), c = a + b / 2, d = a < c - 5 ? a : b;
或
d = (a = rand(), ++a, b = rand(), c = a + b / 2, a < c - 5 ? a : b);
或
a = rand(), ++a, b = rand(), c = a + b / 2, (a < c - 5 && (d = a, 1)) || (d = b);
毋庸置疑,在实践中语句编程通常会生成更具可读性的 C/C++ 代码,因此我们通常会以非常精确和有限的数量使用表达式编程.但在很多情况下它会派上用场.可接受和不可接受之间的界限在很大程度上取决于个人喜好以及识别和阅读既定习语的能力.
Needless to say, in practice statement programming usually produces much more readable C/C++ code, so we normally use expression programming in very well measured and restricted amounts. But in many cases it comes handy. And the line between what is acceptable and what is not is to a large degree a matter of personal preference and the ability to recognize and read established idioms.
另外说明:该语言的设计显然是针对语句量身定制的.语句可以自由调用表达式,但表达式不能调用语句(调用预定义函数除外).这种情况在 GCC 编译器中以一种相当有趣的方式改变,它支持所谓的 语句表达式" 作为扩展(与标准 C 中的表达式语句"对称).语句表达式"允许用户直接将基于语句的代码插入到表达式中,就像他们可以将基于表达式的代码插入到标准 C 中的语句中一样.
As an additional note: the very design of the language is obviously tailored towards statements. Statements can freely invoke expressions, but expressions can't invoke statements (aside from calling pre-defined functions). This situation is changed in a rather interesting way in GCC compiler, which supports so called "statement expressions" as an extension (symmetrical to "expression statements" in standard C). "Statement expressions" allow user to directly insert statement-based code into expressions, just like they can insert expression-based code into statements in standard C.
另外一个说明:在C++语言中,基于函子的编程起着重要的作用,可以看作是另一种形式的表达式编程".根据当前 C++ 设计的趋势,在许多情况下,它可能被认为优于传统的语句编程.
As another additional note: in C++ language functor-based programming plays an important role, which can be seen as another form of "expression programming". According to the current trends in C++ design, it might be considered preferable over traditional statement programming in many situations.
相关文章