Reputation: 35
#include<stdio.h>
#include<conio.h>
int main (void)
{
int a,b,c,d;
clrscr();
a=3;
b=5;
c=a,b;
d=(a,b);
printf("c=%d",c);
printf("d=%d",d);
getch();
}
I am getting the output as c=3d=5 Can anyone please explain me that how am i getting this output?
Upvotes: 1
Views: 165
Reputation: 1
ur output will be c=3 d=5; to visualise the result u should know the associativity of {=}-assignment operator and {,}-comma operator. ->because assignment (=) takes precedence over comma (,) your first statement c=a,b is equivalent to
(a=3),5;
expressions separated by commas are always evaluated left to right. So the value of an expression
(expr1, expr2 )
is always the value of the last expression. Thus, in your second statement d=(a,b) the result of the right-hand side is d=5...
Upvotes: 0
Reputation: 754860
Your code:
a=3;
b=5;
c=a,b;
d=(a,b);
is treated as if it was written:
a=3;
b=5;
(c=a),b;
d=(a,b);
That's why c == 3
and d == 5
.
A good compiler might warn you about the line containing the assignment to c
; the evaluation of b
does nothing to the state of the computation. Similarly for the evaluation of a
in the expression for d
.
What about
d=(a,b)
— how does it displayd=5
in the output?
The comma operator is the lowest priority of all operators in C. It is also important to remember that the commas in the argument list of a function are not comma operators.
The behaviour of the comma operator exemplified by:
x, y;
is to evaluate the expression x
and discard the result, then evaluate the expression y
(and the overall result is the value of y
). There is a sequence point at the comma operator; that's a refinement that doesn't matter to you now, but may do in the future.
In your expression:
d = (a, b);
the expression a
is evaluated (it's 3) and ignored; then the expression b
is evaluated (it's 5), and that is used as the result of the comma operator expression, and therefore of the parenthesized expression, and therefore the value 5 is assigned to d
.
In contrast, in your expression:
c = a, b;
the LHS of the comma operator is c = a
, so a
is evaluated (it's 3) and assigned to c
. This is then thrown away, and b
is evaluated. The overall expression statement just assigns 3 to c, therefore.
You might well ask, "Why is the comma operator useful?"
There are places where it is useful, such as the initialization and increment sections of a for
loop:
for (i = 0, s = p; *s != '\0'; i++, s++)
{
...
}
There are two comma operators there. The first contains a pair of assignments; the second contains a pair of expressions with side-effects.
Another place where it is sometimes used (though I'd argue it is seldom good style), is:
if (some_variable == A_VALUE)
p = q++, r = z;
This 'saves' having to add braces around the condition body. However, it is not good style to hide assignments like that. The code should be (give or take the placement of braces, which is controversial):
if (some_variable == A_VALUE)
{
p = q++;
r = z;
}
If you like Obfuscated C, there's the International Obfuscated C Code Contest.
If you are writing macros, sometimes the comma operator can be a life-saver.
Upvotes: 6
Reputation: 44374
The question revolves mainly around the statements
c=a,b;
d=(a,b);
The =
operator has lower precendence than the ,
operator, so the first statement means:
(c=a),b;
The ,
operator returns the value of the second operand, so the second statement means:
a,(d=b);
Upvotes: 2
Reputation: 9624
OK, I will doit
c=a,b;
Means do:
c=a;
b;
d=(a,b);
Mean do: a; b; assign the last "result" to d;
"," comma separates two statements. The return of the last statement is the result of the combined statements
Upvotes: 0
Reputation: 272687
Because the comma operator (,
) has lower precedence than the assignment operator (=
). So your first example is equivalent to:
(c=a),b;
See here: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence.
Upvotes: 4