Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, why expression(Statement) containing comma(,) operator work differently

I have a simple C code and big confusion about expressions containing comma(,) operator(s).

int main(){
    int i=0,j=11,c;
    c=i=j,++i;
    printf("c=%d i=%d\n",c,i);
    c=(i=j,++i);
    printf("c=%d i=%d\n",c,i);
    return 0;
}

The above code prints:

c=11 i=12
c=12 i=12

My questions are:

  1. What is the actual work of comma(,) as an operator?
  2. ++ has more precedence than , and =, why evaluation is done for the expression on left of comma?
  3. What will be the order if an expression contains operators with different priority, will it depend on comma(,)?
  4. Is it behaving like a substitute of semicolon(;)?
like image 289
skyconfusion Avatar asked Mar 03 '16 09:03

skyconfusion


People also ask

How does comma operator work in C?

The comma operator in c comes with the lowest precedence in the C language. 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 are the different uses of an operator comma?

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.

Is comma a special character in C?

In the C and C++ programming languages, the comma operator (represented by the token , ) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations.

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.


3 Answers

The assignment operator has a higher priority then the comma operator. Thus expression

c = i = j, ++i;

is equivalent to

( c = i = j ), ++i;

According to the C Standard (6.5.17 Comma operator)

2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.114)

In the expression above the result of the comma operator is discarded but it has a side effect of increasing i.

In this expression

c = ( i = j, ++i );

due to using parentheses you changed the order of the evaluation of the above expression. Now it is equivalent to

c = ( ( i = j ), ++i );

and variable c gets the value of expression ++i according to the quote from the C Standard listed above.

like image 162
Vlad from Moscow Avatar answered Sep 28 '22 12:09

Vlad from Moscow


operator comma is to execute many statement and return only result of last statement.

So for c=i=j,++i; : c=i=j is executed, then ++i and after that result of ++i is returned (but not used).

And for c=(i=j,++i);, according to operator precedence, i=j is executed, and just after ++i is executed, and then affectation to c of result of (i=j, ++i), which is the result of last statement, i.e. ++i

So, the behavior of comma is not really same as semicolon. You can use it as a substitute like in c=i=j,++i;.

Personally, I do not encourage to use this operator, which generates less readable and less maintainable code

like image 23
Garf365 Avatar answered Sep 28 '22 12:09

Garf365


What is the actual work of comma(,) as an operator?

The comma operator is mainly a superfluous feature. See this for a description of how it works.

++ has more precedence than , and =, why evaluation is done for the expression on left of comma? What will be the order if an expression contains operators with different priority, will it depend on comma(,)?

The left operand is evaluated for side effects. The result of the comma operator is the result of the evaluated right operand. Note that the comma operator has the lowest precedence of all operators in C.

Is it behaving like a substitute of semicolon(;)?

Kind of, yeah. Both a semi-colon and the comma operator includes a sequence point. The difference is that the comma operator isn't the end of a statement, so it can be squeezed in with other operators on the same line, and it also returns a result.

There is really no reason why you ever would want to do this though. The main use of the comma operator is to obfuscate code, it should be avoided. The only reason why you need to learn how it works, is because you might encounter crap code containing it.

For example, your nonsense code should be rewritten into something more readable and safe:

int main(){
    int i=0;
    int j=11;
    int c;

    i=j;
    c=j;
    i++;
    printf("c=%d i=%d\n",c,i);

    i=j;
    i++;
    c=i;
    printf("c=%d i=%d\n",c,i);

    return 0;
}
like image 45
Lundin Avatar answered Sep 28 '22 13:09

Lundin