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?
- 9
(data = POC_P_Status), TE_OK;assignment has higher precedence than comma.Daniel Fischer– Daniel Fischer2013-07-18 12:59:48 +00:00CommentedJul 18, 2013 at 12:59 - 6@BartFriederichs Precedence is how you tell that the first operand of
,isdata = POC_P_Statusrather than justPOC_P_Status.zwol– zwol2013-07-18 13:02:47 +00:00CommentedJul 18, 2013 at 13:02 - 1@Bart But you determine what are its first and second operands by the rules of precedence.Daniel Daranas– Daniel Daranas2013-07-18 13:03:06 +00:00CommentedJul 18, 2013 at 13:03
- 2@DanielDaranas Certainly the update expression in a for-loop gets an exemption?Daniel Fischer– Daniel Fischer2013-07-18 13:07:15 +00:00CommentedJul 18, 2013 at 13:07
- 2@PeterTry and see. Also
@allin comments doesn't do anything meaningful.millimoose– millimoose2013-07-18 13:17:48 +00:00CommentedJul 18, 2013 at 13:17
2 Answers2
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.
3 Comments
return something is not an expression, soreturn (a,b); is the only possible way to parse it.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, bThea is evaluated and thrown away, and thenb is evaluated. The value of the whole expression is equal tob:
(a, b) == bComma 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 operator1 Comment
x = y, z do?"Explore related questions
See similar questions with these tags.




