difficulty in understanding the mechanism, of the program

Now here are codes of two programs similar to each other, yet different in assigning value to an integer type variable...

PROGRAM1:

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
b=3;
a=2;
c = (a,b); // assingning done through brackets
cout<<c<<endl;
system("pause");
return 0;
}

OUTPUT1: 3


PROGRAM2:

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
b=3;
a=2;
c = a,b;// no brackets are being used
cout<<c<<endl;
system("pause");
return 0;
}

OUTPUT 2: 2

I can't understand how is the assigning of int variable c done in the two problems given above...please help
It doesn't matter.
Do you know how the comma operator works? If not, you can read about it here:
http://www.cplusplus.com/doc/tutorial/operators/

On that page, there's also an operator precedence table, which shows you that operator = has higher priority than the comma operator.
So c = a,b is the same as (c = a), b?
true that, = has higher precedence than , but still why do the brackets change the output of the program?
Because brackets allow you to override the order of precedence.
They're actually (parenthesis), not [brakets]

But yeah, you're changing the order of operations with the parenthesis:

 
c = (a,b);


This line assigns c to whatever (a,b) results in... which will be b, because (a,b) == b

Therefore c = (a,b); = c = b;

Whereas:

 
c = a,b;


= has a higher priority than the , operator, therefore you get:
(c = a),b; which = c = a;


So c is being set to two different things each time. Resulting in two different outputs.
thanks:)
Topic archived. No new replies allowed.