Why does the expression (Statement) containing the comma operator (,) work differently in C?

I have simple C code and a lot of confusion in expressions containing a comma (,) operator (s).

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

The above code prints:

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

My questions:

  • What is the actual work of the comma (,) as an operator?
  • ++ has a higher priority than , and = , why is the evaluation performed for the expression to the left of the comma?
  • What will be the order if the expression contains operators with different priority, does it depend on a comma (,)?
  • Is this behavior like replacing a semicolon (;)?
+7
c operators operator-keyword comma
source share
5 answers

An assignment operator takes precedence over a comma operator. So the expression

 c = i = j, ++i; 

equivalently

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

In accordance with standard C (operator 6.5.17 Comma)

2 The left operand of the comma operator is evaluated as a void expression; there is a sequence point between its evaluation and 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 has the side effect of increasing i .

In this expression

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

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

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

and the variable c gets the value of the expression ++i in accordance with the quote from the standard C.

+10
source share
Operator comma

must execute many statements and return only the result of the last statement.

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

And for c=(i=j,++i); , in accordance with the priority of the operator, i=j executed and immediately after the execution of ++i , and then the affectation to c result (i=j, ++i) , which is the result of the last operator, i.e. ++i

So, the behavior of the semicolon is actually not the same as the semicolon. You can use it as a replacement, as in c=i=j,++i; .

Personally, I do not recommend using this operator, which generates less readable and less maintainable code.

+4
source share

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

The comma operator is basically redundant. See this for a description of how this works.

++ has a higher priority than, and =, why is the evaluation performed for the expression to the left of the comma? What will be the order if the expression contains operators with different priority, does it depend on a comma (,)?

The left operand is evaluated by side effects. The result of the comma operator is the result of the evaluated right operand. Note that a semicolon operator has the lowest priority of all operators in C.

Does it behave as a substitute for a semicolon (;)?

Look, yes. Comma and semicolon operators include a sequence point. The difference is that the comma operator is not the end of the instruction, so it can be compressed with other operators on the same line, and also return the result.

There really is no reason why you would ever want to do this. The main use of the comma operator is code obfuscation, which should be avoided. The only reason you need to find out how this works is because you might run into the shitty code that contains it.

For example, your meaningless 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=%di=%d\n",c,i); i=j; i++; c=i; printf("c=%di=%d\n",c,i); return 0; } 
+2
source share

Ok, let it smash it. In the first case, c and I take the value j => c = i = j = 11; then you increase i => i = 12; So the code is equivalent to this

 c = j; i = j; ++i; 

For the second case, I take the value j => i = j = 11, and then you increase i => i = 12, and then c takes the value i => c = 12;

So the code is equivalent to this:

 i = j; ++i; c = i; 
+1
source share

The comma operator will evaluate and discard all operations, up to the final operation, but not including them. This allows you to use any number of operations, not related to the sequence, in one line, where only the last operation is interesting.

Think of it this way, if you have several loop variables to increase anywhere in the loop, you can separate all additions / subtractions, etc. above your individual variables on a separate line, but why? Where they are executed (within reason), it does not matter for the functioning of the code. Then they can be called on one line without any adverse effect on the code.

+1
source share

All Articles