comma
大家思考這個迴圈:
for(i=3,j=0; i<8,j<11; i++,j++)
實際的情形是這樣
for(i=3,j=0; i<8 && j<11; i++,j++)
還是這樣
for(i=3,j=0; i<8 ||  j<11; i++,j++)
還是都不是呢?
答案都不是哦! 來參考journeyman提供的參考資料:

Comma punctuator and operator     ( , )
.....
The comma is also used as an operator in comma expressions. ...
                                                                                
Syntax:  expression , assignment-expression
EX:  E1,E2=i;		<==作者加入的
                                                                                
The left operand E1 is evaluated as a void expression, then E2
is evaluated to give the result and type of the comma expression.
By recursion, the expression
                                                                                
  E1, E2, ..., En
                                                                                
results in the left-to-right evaluation of each Ei, with the
value and type of En giving the result of the whole expression.
                                                                                
To avoid ambiguity with the commas used in function argument
and initializer lists, parentheses must be used. For example,
                                                                                
  func(i, (j = 1, j + 4), k);
                                                                                
calls func with three arguments, not four. The arguments are i, 5, and k.

簡略說明一下
在comma expression中,comma被視為一個運算字(operator)

語法: expression , assignment-expression
EX:  E1,E2=i;
左邊的運算元(operand)E1被當作void expression來處理,
而E2所處理出來的值與型態是才是整個comma expression真正的結果
那相同的道理:    E1, E2, ..., En
整個comma expression的值與型態依據最後一個En來決定,而其它的Ei,i=0~n-1
只被視為是一個void expression而以,並不會影響comma expression的結果
為了避免在函式的引數中使用comma而產生模糊的狀況,一定要使用小括弧:
EX:	  func(i, (j = 1, j + 4), k);
這個函數有三個引數,並不是四個,而引數的值分別為 i , 5 , k
現在應該有能力來判斷以下的程式有什麼差別了吧!

請考慮以下程式的差別:
for(i=3,j=0; i<8,j<11; i++,j++) printf("i=%d, j=%d\n", i, j);  (1)
                                                                                
for(i=3,j=0; j<11,i<8; i++,j++) printf("i=%d, j=%d\n", i, j);  (2)


參考文獻:
MSDN:C/C++ Language References 
感謝網友:journeyman的糾正與資料
回目錄
Written By James On 2004/02/08 

Hosted by www.Geocities.ws

1