2015-10-17 37 views
-5

通常,C中的所有函數參數用逗號分隔,例如逗號,printf("Data",&a);爲什麼分號用於循環而不是昏迷?

但是對於for循環參數用分號分隔(for(i=0;i<5;++i))爲什麼會這樣?

+2

1.語句由semicola分隔。 2.由此產生的不明確性會產生問題。 3.因爲C標準是這樣說的。 – Downvoter

+5

'for'不是函數,它是語言的關鍵字。 – jbm

回答

4

因爲comma是操作者和允許人們不喜歡的東西

for (i=0,j=5; i<5; ++i, ++j) 

PS:for是一個關鍵字的不高於

**ISO/IEC 9899:1999 §6.8.5.3 The for statement** 

The statement 

for (clause-1 ; expression-2 ; expression-3) statement 

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. 

表達如指出的函數expression-在每次執行循環體之後,3被評估爲空表達式。如果子句-1是一個聲明,則它聲明的任何變量的範圍是聲明 的剩餘部分和整個循環,包括其他兩個表達式;在 控制表達式的第一次評估之前按執行順序達到 。如果條款-1是一個表達式,它被評價 作爲控制 expression.133的第一評估之前的空隙表達)

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant. 

133) Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use in the loop; the 

控制表達,表達-2,則指定的評價每次迭代之前作出 ,使得循環的執行繼續直到 表達式比較等於0;並且表達式-3指定在每次迭代之後執行的操作(例如遞增)。

+0

你有參考嗎?這似乎是不太可能的原因。 – juanchopanza