Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understand comma operator

int main()
{
    int a = (1,2,3);
    int b = (++a, ++a, ++a);
    int c= (b++, b++, b++);
    printf("%d %d %d", a,b,c);
}

I am beginner in programming. I am not getting how does this program shows me output of 6 9 8.

like image 401
sagar Avatar asked Dec 08 '15 11:12

sagar


People also ask

What kind of operator is comma?

The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

What is comma operator in C with example?

Comma in C and C++ 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.

How does comma operator work in Python?

On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.

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.


3 Answers

The , used in all the three declarations

int a = (1,2,3);
int b = (++a, ++a, ++a);
int c = (b++, b++, b++);  

are comma operator. It evaluates the first operand1 and discard it, then evaluates the second operand and return its value. Therefore,

int a = ((1,2), 3);          // a is initialized with 3.
int b = ((++a, ++a), ++a);   // b is initialized with 4+1+1 = 6. 
                             // a is 6 by the end of the statement
int c = ((b++, b++), b++);   // c is initialized with 6+1+1 = 8
                             // b is 9 by the end of the statement.

1 Order of evaluation is guaranteed from left to right in case of comma operator.

like image 105
haccks Avatar answered Sep 18 '22 12:09

haccks


The code is not in any way good and nobody in their right mind would ever write it. You should not spend any time in looking at that kind of code, but I will still give an explanation.

The comma operator , means "do the left one, discard any result, do the right one and return the result. Putting the parts in parentheses doesn't have any effect on the functionality.

Written more clearly the code would be:

int a, b, c;

a = 3; // 1 and 2 have no meaning whatsoever

a++;
a++;
a++;
b = a;

b++;
b++;
c = b;
b++;

The pre- and post-increment operators have a difference in how they act and that causes the difference in values of b and c.

like image 31
Sami Kuhmonen Avatar answered Sep 19 '22 12:09

Sami Kuhmonen


I am beginner in programming. I am not getting how does this program shows me output of

Just understand comma operators and prefix ,postfix .

according to rules mentioned in links given to you

int a = (1,2,3);          // a is initialized with 3 last argument .
int b = (++a, ++a, ++a);  // a is incremented three time so becomes 6 and b initilized with 6 . 
int c = (b++, b++, b++);  // b incremented two times becomes 8  and c initialized with  8.
                          // b incremented once more time becomes 9
like image 24
Mohan Avatar answered Sep 18 '22 12:09

Mohan