2

In a given code I found following sequence,

data = POC_P_Status, TE_OK;

I don't understand what that does mean.

Does the data element receive the first or the second element or something else?

Update:

I read somewhere that this behavior is like this,

if i would write that:

if(data = POC_P_Status, TE_OK) { ... }

then teh if clause will be true if TE_OK is true.

What do you mean?

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
Peter
  • 1,629
  • 2
  • 25
  • 45
  • 9
    `(data = POC_P_Status), TE_OK;` assignment has higher precedence than comma. – Daniel Fischer Jul 18 '13 at 12:59
  • @DanielFischer according to the wikipedia page it has nothing to do with precedence. It is how the operator works: "evaluates its first operand and discards the result, and then evaluates the second operand and returns this value " – Bart Friederichs Jul 18 '13 at 13:01
  • 6
    @BartFriederichs Precedence is how you tell that the first operand of `,` is `data = POC_P_Status` rather than just `POC_P_Status`. – zwol Jul 18 '13 at 13:02
  • 1
    @Bart But you determine what are its first and second operands by the rules of precedence. – Daniel Daranas Jul 18 '13 at 13:03
  • 1
    @BartFriederichs The precedence determines the syntax tree, whether it's `(data = POC_P_Status), TE_OK;` or `data = (POC_P_Status, TE_OK);`. If the comma operator had a higher precedence than assignment, it would be the second. – Daniel Fischer Jul 18 '13 at 13:03
  • 1
    You can always [try and see](http://ideone.com/zIV0ej). – millimoose Jul 18 '13 at 13:03
  • 1
    This question is yet another reason no to use the comma operator. – Daniel Daranas Jul 18 '13 at 13:04
  • 2
    @DanielDaranas Certainly the update expression in a for-loop gets an exemption? – Daniel Fischer Jul 18 '13 at 13:07
  • @DanielFischer See the interesting discussion [in this SO answer](http://stackoverflow.com/a/1232195/96780). – Daniel Daranas Jul 18 '13 at 13:13
  • @DanielFischer I thought you were talking about precedence rules, not about my recommendation no to use the comma operator. In "for" I find it acceptable, because it is intuitive. However, I still prefer update expressions _without_ the comma operator. – Daniel Daranas Jul 18 '13 at 13:16
  • 2
    @Peter [**Try and see.**](http://ideone.com/xezgIc) Also `@all` in comments doesn't do anything meaningful. – millimoose Jul 18 '13 at 13:17
  • @DanielDaranas Ah, I wondered. Yes, I was referring to the use. – Daniel Fischer Jul 18 '13 at 13:20
  • possible duplicate of [What does the comma operator \`,\` do in C?](http://stackoverflow.com/questions/52550/what-does-the-comma-operator-do-in-c) – Mohit Jain Jul 09 '15 at 13:11

2 Answers2

7

It stores POC_P_Status into data.

i = a, b;   // stores a into i.

This is equivalent to

(i = a), b;

because the comma operator has lower precedence than assignment.

Mat
  • 202,337
  • 40
  • 393
  • 406
Pradit
  • 709
  • 5
  • 12
  • 27
  • 1
    Why in a return statement like: return a, b; it will always return b ? Comma operator have higher precedence in this case ? – dotixx Jul 18 '13 at 13:15
  • @dotixx, there is no assignment there, so no precedence issue. – Simon Richter Jul 18 '13 at 13:18
  • 2
    @dotixx The comma operator takes two expressions as operands. `return something` is not an expression, so `return (a,b);` is the only possible way to parse it. – Daniel Fischer Jul 18 '13 at 13:22
5

It's equivalent to the following code:

data = POC_P_Status;
TE_OK;

In other words, it assigns POC_P_Status to data and evaluates to TE_OK. In your first case, the expression stands alone, so TE_OK is meaningful only if it's a macro with side effects. In the second case, the expression is actually part of an if statement, so it always evaluates to the value of TE_OK. The statement could be rewritten as:

data = POC_P_Status;
if (TE_OK) { ... }

From the C11 draft (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf) :

The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value. If an attempt is made to modify the result of a comma operator or to access it after the next sequence point, the behavior is undefined.

That means that in the expression:

a, b

The a is evaluated and thrown away, and then b is evaluated. The value of the whole expression is equal to b:

(a, b) == b

Comma operator is often used in places where multiple assignments are necessary but only one expression is allowed, such as for loops:

for (int i=0, z=length; i < z; i++, z--) {
    // do things
}

Comma in other contexts, such as function calls and declarations, is not a comma operator:

int func(int a, int b) {...}
              ^
              |
              Not a comma operator

int a, b;
     ^
     |
     Not a comma operator
Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29