C 逗号运算符的使用

Uses of C comma operator(C 逗号运算符的使用)
本文介绍了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.

这篇关于C 逗号运算符的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255)
How to convert an enum type variable to a string?(如何将枚举类型变量转换为字符串?)
When to use inline function and when not to use it?(什么时候使用内联函数,什么时候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相关吗?)