简体   繁体   English

逗号作为分隔符和运算符

[英]Comma as a separator and operator

So I came across this question somewhere:所以我在某处遇到了这个问题:

Case 1:情况1:

int a;
a = 1, 2, 3;
printf("%d", a);

Case 2:案例2:

 int a = 1, 2, 3;
 printf("%d", a);

The explanation says:解释说:

The second case gives error because comma is used as a separator, In first case = takes precedence over , so it is basically (a=1), 2, 3 ;第二种情况会出错,因为逗号用作分隔符,第一种情况=优先于,因此基本上是(a=1), 2, 3

But I want to ask why does = not take precedence over , in Case 2?但我想问一下为什么=不优先于,在案例 2 中?

It is not just a question of precedence, but rather a question of the language grammar: the = in both cases is not the same operator:这不仅仅是一个优先级的问题,而是一个语言语法的问题:这两种情况下的=不是同一个运算符:

  • in the declaration int a = 1, 2, 3;在声明中int a = 1, 2, 3; , the = token introduces an initializer which cannot be a comma expression. =标记引入了一个不能是逗号表达式的初始化程序。 The , ends the initializer and the compiler issues an error because 2 is not a valid identifier for another variable. ,结束初始化程序并且编译器发出错误,因为2不是另一个变量的有效标识符。

  • in the statement a = 1, 2, 3;在语句中a = 1, 2, 3; , a = 1, 2, 3 is an expression, parsed as ((a = 1), 2), 3 because = has higher precedence than , . , a = 1, 2, 3是一个表达式,解析为((a = 1), 2), 3因为=优先级高于, = is the assignment operator whose right hand side is an expression, this assignment is the left operand of a comma operator , followed by a constant expression 2 , a = 1, 2 itself the left operand of the final , operator whose right operand is 3 . =是赋值运算符,其右手边是一个表达式,此分配是逗号操作者的左操作数,后跟一个常量表达式2a = 1, 2本身最终的左操作数,操作者,其右操作数是3 . The statement is equivalent to ((a = 1), 2), 3);该语句等价于((a = 1), 2), 3); , which simplifies into a = 1; , 简化为a = 1; . .

This这个

int a = 1, 2, 3;/* not a valid one */

is wrong because since = has higher priority, so it become int a = 1 internally and there is no name for 2 and 3 thats why this statement is not valid and cause compile time error.是错误的,因为=具有更高的优先级,所以它int a = 1内部变为int a = 1并且没有23名称,这就是为什么此语句无效并导致编译时错误的原因。

To avoid this you might want to use为避免这种情况,您可能需要使用

int a = (1, 2, 3); /* evaluate all expression inside () from L->R and assign right most expression to a i.e a=3*/

And here和这里

int a;
a = 1,2,3; 

there are two operator = and , and see man operator .有两个 operator =,参见man operator The assignment operator = has higher priority than comma operator.赋值运算符=优先级高于comma运算符。 So it becomes a=1 .所以它变成a=1

a = 1,2,3;
    | L--->R(coma operator associativity) 
    this got assigned to a

for eg例如

int x = 10, y = 20,z;
z = 100,200,y=30,0; /* solve all expression form L to R, but finally it becomes z=100*/ 
printf("x = %d y = %d z = %d\n",x,y,z);/* x = 10, y = 30(not 20) z = 100 */
z = (100,200,y=30,0); /* solve all expression form L to R, but assign right most expression value to z*/ 

Inside variable declarations (as case 1) comma are used to declare several variables, for example:内部变量声明(如情况 1)逗号用于声明多个变量,例如:

int a,b=2,c=b+1,d; //here only b and c were initialized

An statement in C/C++ could be a list of comma separated expressions (this is what happens in case 2): C/C++ 中的语句可以是逗号分隔的表达式列表(这是在情况 2 中发生的情况):

a=b+1, c+=2, b++, d = a+b+c, 3, d; //these are expressions, remember one literal is an expression too!!!

NOTE : comma (,) is a compile time operator ,注意:逗号 (,) 是编译时运算符
from my side their is Four cases that you can come across :在我看来,他们是您可以遇到的四种情况
case 1情况1

int a = 1, 2, 3; // invalid case cause too many initializers

case 2案例2

int a = (1, 2, 3); // valid case
/* You can expand this line as a :
   1;
   2;
   int a = 3;
*/

case 3案例3

int a;
a = 1, 2, 3; // valid case
/* You can expand this line as a : 
   a = 1; // expression 1
   2;     // expression 2            
   3;     // expression 3
*/

case 4案例四

int a;
a = ( 1, 2, 3);// valid case
/* You can expand this line as a :
   1;     // expression 1
   2;     // expression 2
   a = 3; // expression 3
*/  

In above cases in place of 1, 2, 3 we can use any valid expression in C, explore more!!在上述情况下,我们可以使用 C 中的任何有效表达式代替 1、2、3,探索更多!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM