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's user avatar
Daniel Daranas
22.7k10 gold badges66 silver badges122 bronze badges
askedJul 18, 2013 at 12:58
Peter's user avatar
13
  • 9
    (data = POC_P_Status), TE_OK; assignment has higher precedence than comma.CommentedJul 18, 2013 at 12:59
  • 6
    @BartFriederichs Precedence is how you tell that the first operand of, isdata = POC_P_Status rather than justPOC_P_Status.CommentedJul 18, 2013 at 13:02
  • 1
    @Bart But you determine what are its first and second operands by the rules of precedence.CommentedJul 18, 2013 at 13:03
  • 2
    @DanielDaranas Certainly the update expression in a for-loop gets an exemption?CommentedJul 18, 2013 at 13:07
  • 2
    @PeterTry and see. Also@all in comments doesn't do anything meaningful.CommentedJul 18, 2013 at 13:17

2 Answers2

7

It storesPOC_P_Status intodata.

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

This is equivalent to

(i = a), b;

because the comma operator has lower precedence than assignment.

Mat's user avatar
Mat
208k41 gold badges407 silver badges423 bronze badges
answeredJul 18, 2013 at 13:00
Pradit's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

Why in a return statement like: return a, b; it will always return b ? Comma operator have higher precedence in this case ?
@dotixx, there is no assignment there, so no precedence issue.
@dotixx The comma operator takes two expressions as operands.return something is not an expression, soreturn (a,b); is the only possible way to parse it.
5

It's equivalent to the following code:

data = POC_P_Status;TE_OK;

In other words, it assignsPOC_P_Status todata and evaluates toTE_OK.In your first case, the expression stands alone, soTE_OK is meaningful only if it's a macro with side effects. In the second case, the expression is actually part of anif statement, so it always evaluates to the value ofTE_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

Thea is evaluated and thrown away, and thenb is evaluated. The value of the whole expression is equal tob:

(a, b) == b

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

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

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

int func(int a, int b) {...}              ^              |              Not a comma operatorint a, b;     ^     |     Not a comma operator
answeredJul 18, 2013 at 13:21
Ilmo Euro's user avatar

1 Comment

While this is all true, it doesn't actually answer the question which was "What doesx = y, z do?"

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.