久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C 逗號(hào)運(yùn)算符的使用

Uses of C comma operator(C 逗號(hào)運(yùn)算符的使用)
本文介紹了C 逗號(hào)運(yùn)算符的使用的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

你看到它在 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
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 仍然相關(guān)嗎?)
主站蜘蛛池模板: 欧美日韩精品一区二区三区视频 | 国产精品国产成人国产三级 | 91久久北条麻妃一区二区三区 | 精品一区国产 | 黄色国产在线视频 | 四虎影院久久 | 成人一区二区三区在线观看 | 人人澡人人射 | 操一草| 黄色网址在线免费播放 | 欧美日韩视频 | 天天色官网 | 久久99深爱久久99精品 | 国产精品视频一二三区 | 久久精品伊人 | 亚洲欧洲精品在线 | 精品国产视频 | 18成人在线观看 | 成人在线精品视频 | 亚洲欧美综合 | 国产成人精品一区二区三区网站观看 | 欧美1级 | 国产一级大片 | 亚洲精品99久久久久久 | 岛国毛片 | 国产福利91精品一区二区三区 | 青青草一区二区三区 | 无吗视频 | 国产精久久久久久 | 一区二区精品电影 | 久久久久久久久中文字幕 | 自拍视频网站 | 久久久久久亚洲精品 | 日韩在线电影 | 狠狠的干狠狠的操 | 婷婷91| 视频一区二区在线观看 | 中文字幕在线观看一区二区 | 亚洲欧美中文日韩在线v日本 | 国产中的精品av涩差av | 91在线色视频 |