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

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 逗號運算符的使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 仍然相關嗎?)
主站蜘蛛池模板: 国产精品毛片va一区二区三区 | 99精品网站| 一级片观看 | 国产精品综合 | 视频在线一区 | 国产精品主播一区二区 | 91亚洲国产成人精品性色 | 伊人av在线 | 国产欧美日韩一区二区三区 | 国产精品毛片久久久久久久 | 成人亚洲视频 | 热久久免费视频 | www4h| 日本视频一区二区三区 | 国产一区二区三区免费 | 亚洲精品www久久久久久广东 | 男人的天堂亚洲 | 日韩综合精品 | 精品国产伦一区二区三区 | 国产美女视频网站 | 国产午夜三级 | 亚洲精品社区 | 国产精品视频久久 | 黄av在线| 午夜网 | 亚洲在线中文字幕 | 亚洲欧美日韩一区 | 91丨九色丨蝌蚪丨丝袜 | 亚洲www | 天堂影院av | a级黄色片| 视频一区在线播放 | 国产精品一区二区不卡 | 我要看一级片 | 亚洲图片一区二区 | 99国产精品99久久久久久粉嫩 | 中文字幕一区二区三区在线观看 | 国产寡妇亲子伦一区二区三区四区 | 久久亚洲国产精品 | 日韩二区三区 | 亚洲av毛片成人精品 |