ç逗号操作符的使用 [英] Uses of C comma operator

查看:146
本文介绍了ç逗号操作符的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您看到它用于在循环语句,但它是合法的语法的任何地方。有什么样的用途,你发现它在其他地方,如果有的话?

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 ++)在历史上是两个完全不同的编程风格,这可以称之为语句编程和前$组合p $ pssion编程。如你所知,每一个过程编程语言通常支持等基本构造为的和的分支的。这些基本结构是在C / C ++语言present有两种形式:一种为声明编程,另一个是前pression编程

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. These fundamental constructs are present in C/C++ languages in two forms: one for statement programming, another for expression programming.

例如,当你在语句的形式编写程序,你可以使用由分隔的语句序列; 。当你想要做一些分支,可以使用如果语句。您还可以使用的控制转移语句循环等多种。

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.

在EX pression编程相同的结构提供给你。这实际上是运营商进场。操作在没有其他比C,即运营商顺序前pressions的前$分隔p $ pssion编程作为同样的作用; 确实在声明中编程。在EX pression程序分支是通过完成:运营商和,或者,通过&功放短路计算性能;&安培; || 运营商。 (防爆pression编程有没有周期,虽然,并将它们与递归你必须声明应用程序进行更换。)

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.)

例如,下面的code

For example, the following code

a = rand();
++a;
b = rand();
c = a + b / 2;
if (a < c - 5)
  d = a;
else
  d = b;

这是传统的语句编程的一个例子,可重新写入的前pression编程条款

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 ++ code,所以我们通常使用前pression编程很好的衡量和限制金额。但在许多情况下,它来得心应手。之间有什么是可以接受的,什么是不是很大程度的个人preference和认读成语建立能力的事就行了。

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.

作为一个附加说明:语言的设计很明显是朝着报表定制。声明可以自由调用前pressions,但前pressions不能调用语句(除了调用pre定义的函数)。这种情况在GCC编译一个相当有趣的方式,它支持所谓的的声明前改变pressions作为扩展(对称,在标准C的前pression声明)。 声明前pressions允许用户直接插入基于语句的code到前pressions,就像他们可以插入前$ P $基于pssion,code到标准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 ++语言基于仿编程起着重要的作用,它可以被看作是前pression编程的另一种形式。根据C ++设计目前的趋势,这可能被认为是比传统的语句编程preferable在许多情况下。

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.

这篇关于ç逗号操作符的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆