-1

I'm unable to understand the working of this piece of code.

#include<stdio.h>
void main(){
  int a,b;
  a=3,1;
  b=(5,4);
  printf("%d",a+b);
}  

The output is 7. What is that assignment?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 1
    See [What does the comma operator `,` do in C?](http://stackoverflow.com/q/52550/1708801) – Shafik Yaghmour Mar 23 '15 at 19:28
  • Don't ever do this in real life! Also, please use whitespace. – Fiddling Bits Mar 23 '15 at 19:32
  • Note, I realized the question although canonical did not address this particular issue so I updated my answer to the linked question above to also cover this issue as well. – Shafik Yaghmour Mar 23 '15 at 19:36
  • If you use the debugger, you will see the values assigned to `a` and `b`, this will help you to understand. If you start asking others for thinks like this, you will progress very slowly. – mins Mar 23 '15 at 19:43

3 Answers3

4

Comma operator evaluates its first operand and discards the result, and then evaluates the second operand and returns this value.

After executing

a=3,1;  //  (a = 3), 1;

a will have 3 and after

b=(5,4);  // Discard 5 and the value of the expression (5,4) will be 4

b will have 4.

Some more examples on Wikipedia:

// Examples:               Descriptions:                                                                   Values after line is evaluated:
int a=1, b=2, c=3, i=0; // commas act as separators in this line, not as an  operator 
                        // ... a=1, b=2, c=3, i=0
i = (a, b);             // stores b into i 
                        // ... a=1, b=2, c=3, i=2
i = a, b;               // stores a into i. Equivalent to (i = a), b;
                        // ... a=1, b=2, c=3, i=1
i = (a += 2, a + b);    // increases a by 2, then stores a+b = 3+2 into i
                        // ... a=3, b=2, c=3, i=5
i = a += 2, a + b;      // increases a by 2, then stores a to i, and discards unused
                        // a + b rvalue. Equivalent to (i = (a += 2)), a + b; 
                        // ... a=5, b=2, c=3, i=5
i = a, b, c;            // stores a into i, discarding the unused b and c rvalues
                        // ... a=5, b=2, c=3, i=5
i = (a, b, c);          // stores c into i, discarding the unused a and b rvalues
                        // ... a=5, b=2, c=3, i=3
return a=4, b=5, c=6;   // returns 6, not 4, since comma operator sequence points
                        // following the keyword 'return' are considered a single
                        // expression evaluating to rvalue of final   subexpression c=6
return 1, 2, 3;         // returns 3, not 1, for same reason as previous example
return(1), 2, 3;        // returns 3, not 1, still for same reason as above.  This
                        // example works as it does because return is a keyword, not 
                        // a function call. Even though most compilers will allow for
                        // the construct return(value), the parentheses are syntactic
                        // sugar that get stripped out without syntactic analysis 
haccks
  • 104,019
  • 25
  • 176
  • 264
2
  a=3,1; // (a=3),1 -- value of expression is 1, side effect is changing a to 3
  b=(5,4);
  printf("%d",a+b); // 3 + 4
pmg
  • 106,608
  • 13
  • 126
  • 198
2

In this statement

a=3,1;

two operators are used: the assignment operator and the comma operator. The priority of the assignment operator is greater than the priority of the comma operator, thus this statement is equivalent to

( a = 3 ), 1;

1 is simply discarded, so a is assigned the value 3.

In this statement

b=(5,4);

due to the parentheses the comma operator is evaluated first. Its value is the value of the last expression, 4. So b is assigned the value 4.

As result you get a + b => 3 + 4 which equals 7.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335