3

This has been asked in an interview. What is the output of the below snippet?

#include <iostream>
using namespace std;

int main() {
    cout << (3,2,1)-(1,2,3) << endl; // in C++ too this prints -2
    printf("%d\n",(3,2,1)-(1,2,3)); // prints -2
    printf("%d\n",("%d",3,2,1)-(1,2,3)); // prints -2
    return 0;
}

By the output I am guessing its (1-3) = -2. But how from (3,2,1) value 1 is chosen, similarly from (1,2,3) value 3 is chosen? Am I right in what I am guessing?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
highlander141
  • 1,683
  • 3
  • 23
  • 48
  • The title of your question mentions C, but your example is in C++. Could you make them consistent, one way or the other? – psmears Jul 09 '15 at 09:44
  • 2
    See: [What does the comma operator `,` do in C?](http://stackoverflow.com/questions/52550/what-does-the-comma-operator-do-in-c) – P.P Jul 09 '15 at 09:45

4 Answers4

10

Here, the comma operator and it's property is being used.

To elaborate, from C11, chapter §6.5.17, Comma operator

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value

and, from C++11, chapter § 5.18,

A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded-value expression (Clause 5). Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. The type and value of the result are the type and value of the right operand; the result is of the same value category as its right operand, and is a bit-field if its right operand is a glvalue and a bit-field.

So, in case of a statement like

  (3,2,1)-(1,2,3)

for the evaluation,

  • (3,2,1), 3 and 2 are (evaluated as a void expression and) discarded, 1 is the value.
  • (1,2,3), 1 and 2 are (evaluated as a void expression and) discarded, 3 is the value.

so, the statement reduces to 1 - 3 which is equal to -2.

Same way, you can use for more elements, also.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

(3,2,1) means to calculate all the expressions and return last. So, it does:

  • 3 - nothing
  • 2 - nothing
  • 1 - return 1

And other:

  • 1 - nothing
  • 2 - nothing
  • 3 - return 3

so your

cout << (3,2,1)-(1,2,3) << endl;

means:

cout << 1 - 3 << endl;
VP.
  • 15,509
  • 17
  • 91
  • 161
1

You have to consider the comma operator (,)

The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.

In your case:

(3,2,1) //evaluates to 1
(1,2,3) //evaluates to 3

source: http://www.cplusplus.com/doc/tutorial/operators/

dau_sama
  • 4,247
  • 2
  • 23
  • 30
1

Comma operator always returns the last value i.e.

  1. in your first cout it is 1-3=-2
  2. next, in printf again it is 1-3=-3
  3. finally in last printf, it is again 1-3=-2

Comma operator always solves all the left expressions(operands) and just returns the rightmost operand as result as rvalue.

TryinHard
  • 4,078
  • 3
  • 28
  • 54