Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma as a separator and operator

Tags:

c++

c

So I came across this question somewhere:

Case 1:

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

Case 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;

But I want to ask why does = not take precedence over , in Case 2?

like image 511
Rahul Ranjan Avatar asked Jul 01 '18 04:07

Rahul Ranjan


People also ask

Is comma an operator?

1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.

What is comma and sizeof operator?

The comma operator has no special meaning to sizeof . sizeof(b, a) examines the complete expression (b, a) , works out the resultant type, and computes the size of that type without actually evaluating (b , a) .

What are the uses of comma and conditional operator?

The comma operator (,) allows you to evaluate multiple expressions wherever a single expression is allowed. The comma operator evaluates the left operand, then the right operand, and then returns the result of the right operand.

Which operator works on list of comma separated values?

It is comma operator.


3 Answers

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;, 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.

  • in the statement a = 1, 2, 3;, a = 1, 2, 3 is an expression, parsed as ((a = 1), 2), 3 because = has higher precedence than ,. = 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. The statement is equivalent to ((a = 1), 2), 3);, which simplifies into a = 1;.

like image 140
chqrlie Avatar answered Oct 15 '22 13:10

chqrlie


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.

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. The assignment operator = has higher priority than comma operator. So it becomes a=1.

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

for e.g

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*/ 
like image 25
Achal Avatar answered Oct 15 '22 14:10

Achal


Inside variable declarations (as case 1) comma are used to declare several variables, for example:

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):

a=b+1, c+=2, b++, d = a+b+c, 3, d; //these are expressions, remember one literal is an expression too!!!
like image 35
Eduardo Pascual Aseff Avatar answered Oct 15 '22 12:10

Eduardo Pascual Aseff