2

Can I pass multiple values to the switch case statement separating different values by commas ? what would the expression evaluate to? If no error occurs, which of the multiple values would the compiler evaluate the switch with ? Keeping in mind the low precedence of the comma operator? say for instance i write the code as follows:

int m=10;
switch(m, m*2)
{
     case 10: printf("\n case 10");
     case 20: printf("\n case 20");
     case 30: printf("\n case 30");
     default: printf("\n no case");
 }
4

1 回答 1

6

逗号运算符计算所有表达式并返回最右边的值。(在 . 中使用它没有任何区别switch。)

您的示例与 完全相同switch (m*2),但如果前面的任何表达式有副作用,则这些副作用将在switch语句执行之前应用。

于 2013-08-18T19:21:57.433 回答