What will the following code evaluate?

The comma operator can be used to link the related expressions together. A comma-linked list of expressions is evaluated left-to-right and the value of the rightmost expression is the value of the combined expression. It acts as a sequence point.

The comma operator can be used to link the related expressions together. A comma-linked list of expressions is evaluated left-to-right and the value of the rightmost expression is the value of the combined expression. It acts as a sequence point.

A sequence point guarantees that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. So, any expression/assignment will be completed & only then will the next expression to the right be evaluated. For example, b=(a=4,a=5); gives, a=5 b=5 Also, note that the comma operator ranks last in the precedence list of operators in C.

And also that those brackets() were of great worth. – Akito Sep 20 '10 at 12:06 @fahad, yes, that is exactly the use of the "," as a sequence point. – crypto Sep 20 '10 at 12:08.

(i,j) is exactly the same as just j, because I is just a variable and evaluating it doesn't cause any side effect. So basically it's just obfuscation here.

Teaching students why obfuscation is bad :) – pmg Sep 20 '10 at 11:50.

In (i,j), the , operator does nothing because the left-hand side expression does not have side-effects. The assignment is thus equivalent to: j = i? (j?

I : j) : j; And since I and j are non-zero, to j = i.

I : j) : j; The comma operator evaluates to the last expression - so (i, j) == j. That is non-zero, so the center expression evaluates to i. 'i' being non-zero, the outer expression evaluates to i, so j is assigned to the value of i.

This is equivalent to: int main() { int I = 10, j = 20; if (i! = 0) { (void)i; // Do nothing with i. If (j!

= 0) { j = i; } else { j = j; // Assign j to itself. } } else { j = j; // Assign j to itself. } printf("%d %d", j); }.

Looks like typical software written test question. It is used to confuse candidates. As suggested by sepp2k above it is same as j.

One more interview question I = 10; j = 20; 1) k = i,j; 2) k = (i,j); Answer for 1 will be 10 and answer for 2 will be 20. As coma operator doesn't do anything. I hope this will clear more.

Confused! What are those brackets for? And why is the 2nd answer 20?

– Akito Sep 20 '10 at 12:02 1 @fahad Assignment operator has higher precedence than coma operator. So in case 1) K is first assigned with value I and j will be neglected. While in case 2 first bracket will be evaluated and inside bracket evaluation is done from left to right so (i,j) will evaluate to j.

Hence answer will be 20 – Alam Sep 20 '10 at 12:21.

It's not really doing anyting. It's evaluating the expression i, discards the result, evaluates the expression j and returns the result. As evaluating the expression I has no side effects, (i,j) has the same meaning as just j.

If j (1) */ I : /* else */ j ) : /* else */ j; // Added parenthesis and see (2) printf("%d %d", i, j); // Will therefore print 10 10 } it is equivallent to if(j) because (i,j) evaluate to j because I has no side effect. All ifs evaluate to true because I and j are ints and are non zero.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions