問題描述
你看到它在 for 循環(huán)語句中使用,但它在任何地方都是合法的語法.如果有的話,您在其他地方發(fā)現(xiàn)了它的哪些用途?
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++)在歷史上是兩種完全不同的編程風(fēng)格的混合,可以將其稱為語句編程"和表達(dá)式編程"".如您所知,每種過程式編程語言通常都支持諸如排序和分支之類的基本結(jié)構(gòu)(請參閱結(jié)構(gòu)化編程).這些基本結(jié)構(gòu)以兩種形式出現(xiàn)在 C/C++ 語言中:一種用于語句編程,另一種用于表達(dá)式編程.
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.
例如,當(dāng)您根據(jù)語句編寫程序時(shí),您可能會(huì)使用由 ;
分隔的語句序列.當(dāng)你想做一些分支時(shí),你使用 if
語句.您還可以使用循環(huán)和其他類型的控制轉(zhuǎn)移語句.
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.
在表達(dá)式編程中,您也可以使用相同的構(gòu)造.這實(shí)際上是 ,
運(yùn)算符發(fā)揮作用的地方.運(yùn)算符 ,
只不過是 C 中順序表達(dá)式的分隔符,即運(yùn)算符 ,
在表達(dá)式編程中的作用與 ;
在語句中的作用相同編程.表達(dá)式編程中的分支是通過 ?:
運(yùn)算符完成的,或者通過 &&
和 ||
運(yùn)算符的短路評(píng)估屬性完成.(不過,表達(dá)式編程沒有循環(huán).要用遞歸替換它們,您必須應(yīng)用語句編程.)
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;
這是傳統(tǒng)語句編程的一個(gè)例子,在表達(dá)式編程方面可以重寫為
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);
毋庸置疑,在實(shí)踐中語句編程通常會(huì)生成更具可讀性的 C/C++ 代碼,因此我們通常會(huì)以非常精確和有限的數(shù)量使用表達(dá)式編程.但在很多情況下它會(huì)派上用場.可接受和不可接受之間的界限在很大程度上取決于個(gè)人喜好以及識(shí)別和閱讀既定習(xí)語的能力.
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.
另外說明:該語言的設(shè)計(jì)顯然是針對(duì)語句量身定制的.語句可以自由調(diào)用表達(dá)式,但表達(dá)式不能調(diào)用語句(調(diào)用預(yù)定義函數(shù)除外).這種情況在 GCC 編譯器中以一種相當(dāng)有趣的方式改變,它支持所謂的 語句表達(dá)式" 作為擴(kuò)展(與標(biāo)準(zhǔn) C 中的表達(dá)式語句"對(duì)稱).語句表達(dá)式"允許用戶直接將基于語句的代碼插入到表達(dá)式中,就像他們可以將基于表達(dá)式的代碼插入到標(biāo)準(zhǔn) 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.
另外一個(gè)說明:在C++語言中,基于函子的編程起著重要的作用,可以看作是另一種形式的表達(dá)式編程".根據(jù)當(dāng)前 C++ 設(shè)計(jì)的趨勢,在許多情況下,它可能被認(rèn)為優(yōu)于傳統(tǒng)的語句編程.
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.
這篇關(guān)于C 逗號(hào)運(yùn)算符的使用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!