tags:

views:

108

answers:

8

I had the following code in a test.I am confused about what (i,j) evaluates,while reading about the "," operator i found that it just evaluates the variables or functions but what does it do here?

main()
{
    int i = 10, j = 20;

    j = i ? (i, j) ? i : j : j;
    printf("%d %d", i, j);
}
+1  A: 

It will print:

10 10

This is because you can break the expression down like:

j = i ? ((i, j) ? 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.

sje397
+4  A: 

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

sepp2k
teaching students why obfuscation is bad :)
pmg
+3  A: 

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;

Pascal Cuoq
A: 

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.

Guffa
+1  A: 

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);

}
Jon Purdy
A: 
main()
{
    int i = 10, j = 20; // i=10, j=20

    j = i ? /*if i */ ( (i, j) ? /* if j (1) */ i : /* else */ j ) : /* else */ j; // Added parenthesis and see (2)

    printf("%d %d", i, j); // Will therefore print 10 10
}
  1. it is equivallent to if(j) because (i,j) evaluate to j because i has no side effect.
  2. all ifs evaluate to true because i and j are ints and are non zero
Cedric H.
+1  A: 

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 doesnt do anything. I hope this will clear more.

alam
Let me try..1--> k=i and 2--> k=? Confused!What are those brackets for?And why is the 2nd answer 20?
fahad
@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
+2  A: 

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.

Kedar Soparkar
You mean when you proceed left to right,the value of a which was first assigned 4 is over-written as 5?And also that those brackets() were of great worth.
fahad
@fahad, yes, that is exactly the use of the "," as a sequence point.
Kedar Soparkar