Comma operator in c [duplicate] - c

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 5 years ago.
#include<stdio.h>
int main(void) {
int a;
a = (1, 2), 3;
printf("%d", a);
return 0;
}
output: 2
Can any one explain how output is 2?

Can any one explain how output is 2?
Because the precedence of the assignment operator (=) is higher than the comma operator (,).
Therefore, the statement:
a = (1, 2), 3;
is equivalent to:
(a = (1, 2)), 3;
and the expression (1, 2) evaluates to 2.

Can any one explain how output is 2?
In the statement
a = (1, 2), 3;
, used is a comma operator.
Due to higher operator precedence of = operator than that of , operator, the expression operand (1, 2) will bind to = as
(a = (1, 2)), 3;
In case of comma operator, the left operand of a comma operator is evaluated to a void expression, then the right operand is evaluated and the result has the value and type of the right operand.
There are two comma operators here. For the first comma operator in the expression (1, 2), 1 will be evaluated to void expression and then 2 will be evaluated and will be assigned to a.
Now side effect to a has been taken place and therefore the right operand of second comma operator 3 will be evaluated and the value of the expression (a = (1, 2)), 3 will be 3.

the result of:
a = x, y => x
a = (i, j) => j
therefore, if we have:
x = (1 , 2)
a = (1 , 2) , 3 => 2
As said here:
The comma operator separates expressions (which have value) in a way
analogous to how the semicolon terminates statements, and sequences of
expressions are enclosed in parentheses analogously to how sequences
of statements are enclosed in braces: (a, b, c) is a sequence of
expressions, separated by commas, which evaluates to the last
expression c while {a; b; c;} is a sequence of statements, and does
not evaluate to any value. A comma can only occur between two
expressions – commas separate expressions – unlike the semicolon,
which occurs at the end of a (non-block) statement – semicolons
terminate statements.
The comma operator has the lowest precedence of any C operator, and
acts as a sequence point. In a combination of commas and semicolons,
semicolons have lower precedence than commas, as semicolons separate
statements but commas occur within statements, which accords with
their use as ordinary punctuation: a, b; c, d is grouped as (a, b);
(c, d) because these are two separate statements.
I hope this answers your question.

Related

C: int x = k * (x1, x2, x3)? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
Closed 7 years ago.
What exactly is this called in C?
If I have int x = 3 * (4, 5);
I end up with 15.
Can someone give explanation?
Is this a list where the result is just the first number times the last one?
Thanks
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).
https://en.wikipedia.org/wiki/Comma_operator
So, the result of the (4, 5) expression will be 5.

Using comma operator in c

I have read that comma operator is used to assign expression, and the right expression is supplied to lvalue.
But why does this program assign left expression to lvalue when not using parenthesis? I am using turbo c compiler.
int b=2;
int a;
a=(b+2,b*5); // prints 10 as expected
a=b+2,b*5; // prints 4 when not using parenthesis
Also the following works:
int a =(b+2,b*5);
But this generates an error:
int a =b+2,b*5; // Error
I can't understand why.
Because precedence of , operator is lower than of = one, this...
a=b+2,b*5;
... will actually be evaluated as...
a = b + 2;
b * 5;
With int i = b + 2, b * 5; is a bit different, because comma has different meaning in declaration statements, separating different declarations from each other. Consider this:
int a = 3, b = 4;
You still have comma here, but now it separates two variable assignment-on-declarations. And that's how the compiler attempts to treat that line from your example - but fails to get any meaning from b * 5 line (it's neither assignment nor declaration).
Now, int a = (b + 2, b * 5) is different: you assign a value of b + 2, b * 5 expression to a variable a of type int. The first sub-expression is discarded, leaving you just with b * 5.

Unclear behavior of "," operator in C

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?
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.
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

Difference between "int i=1,2,3" and "int i=(1,2,3)" - variable declaration with comma operator [duplicate]

This question already has an answer here:
Why different behavior in two cases 1st. int i = 1,2,3; and 2nd. int i; i = 1,2,3; [duplicate]
(1 answer)
Closed 7 years ago.
int i=1,2,3;
int i=(1,2,3);
int i; i=1,2,3;
What is the difference between these statements? I can't get to any particular reason for it.
Statement 1 Result : Compile error.
'=' operator has higher precedence than ',' operator.
comma act as a separator here. the compiler creates an integer variable 'i' and initializes it with '1'.
The compiler fails to create integer variable '2' as '2' is not a valid indentifer.
Statement 2 Result: i=3
'()' operator has higher precedence than '='. So , firstly, bracket operator is evaluated. '()' operator is operated from left to right. but it is always the result of last that gets assigned.
Statement 3: Result: i=1
'=' operator has higher precedence than ',' operator. so 'i' gets initialized by '1'. '2' and '3' are just constant expression. so have no effect .
It is the comma operator
i = a, b, c; // stores a into i ... a=5, b=2, c=3, i=5
i = (a, b, c); // stores c into i ... a=5, b=2, c=3, i=3
the differing behavior between the first and second lines is due to the comma operator having lower precedence than assignment.

Are "arithmetic expressions" separated by commas allowed in C or we need separate statements for each?

In C, is the following statement
a+=3,b=c*2,d=a+b;
equivalent to the following block of statements:
a+=3;
b=c*2;
d=a+b;
I am sure you got my point. Can we safely use multiple mathematical expressions separated by commas in the same statement in C? And in what cases this can pose problems?
It might be easier if you think of the comma-expression list you present like this:
((a += 3, b = c * 2), d = a + b)
First the innermost comma-expression is evaluated:
a += 3, b = c * 2
That expression will be evaluated in two steps:
a += 3
b = c * 2
The result of a += 3 will be thrown away by the compiler, but the assignment still happens, it's just that the returned result is thrown away. The result of the first comma expression is b (which will be c * 2 (whatever that is)).
The result of the first comma expression is then on the left-hand side of the next comma expression:
b = c * 2, d = a + b
Which will then be sequenced as
b = c * 2
d = a + b
The result of the expression b = c * 2 is thrown away (but as it is still evaluated the assignment still happens), and the result of the complete expression is d (which is a + b).
The result of the whole expression will be d.
They are the same.
In C, the comma operator evaluates its left hand side, ignores the return value (for example the return of x = y is the new value), evaluates the right hand side and returns its result (if any). The use of the comma operator is to evaluate and get the side effects of what's on the left hand without using (e.g. discarding) its value.

Resources