0
What is this ?
The question was : "What is the output of this code ? int a, b; a = 1, 2 ,3, 4, 5; b = (1, 2, 3, 4, 5); cout << a << b;" The answer was 15... I don't even understand what is a and b, are they int arrays ? If are they what is the difference between them ? Why "cout << a" should output 1 and "cout << b" should output 5 ? What is the C++ notion that I've missed to understand that ? Thanks for constructive answers.
1 Answer
+ 2
The difference between what was assigned for <a> and <b> was that:
a = 1, 2, 3, 4, 5;
Assignment operator takes precedence over comma operator. So 1 be the value assigned for variable <a>. Here assignment operator does its job before the comma operator began working.
b = (1, 2, 3, 4, 5);
The parentheses makes the expression(s) inside it get prioritized and thus be evaluated completely, before the assignment operator does its job. Which in turn, makes the rightmost operand (5) be the one that is assigned as value of variable <b>.
Quoted from link below:
"Comma operator returns the value of the rightmost operand when multiple comma operators are used inside an expression."
http://www.c4learn.com/c-programming/c-comma-operator/