为什么分号用于for循环而不是昏迷?

时间:2015-10-17 11:59:10

标签: c function loops

通常,C中的所有函数参数都用逗号,分隔,例如printf("Data",&a);

但对于for循环,参数用分号(for(i=0;i<5;++i)分隔)为什么会这样?

1 个答案:

答案 0 :(得分: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.
     

表达式-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指定一个   在每个之后执行的操作(例如递增)   迭代。