4

Possible Duplicate:
C++ Comma Operator

that probably a trivial question, but I don't know the answer. And this has been troubling me this afternoon.

I was just writing a function to convert RVB to YUV. Nothing really special, but mistakenly used the comma (,) instead of a dot in my numbers.

It compiles but the result was not what I expected, for example "-3713796" instead of a 0-255 range number.

(0,615*(double) 61) - (0,51498*(double) 61) - (0,10001*(double) 61)

So what does it mean ?
If it's not a compilation error, it's probably usefull for something but what?

Ps: I was using C++ with Qt.

Community
  • 1
  • 1
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134

2 Answers2

7

You've accidentally used the comma operator. It evaluates its first operand and discards the result, and then evaluates the second operand and returns its value and type.

In my experience, it's most commonly used in loop statements. For other uses, see Uses of C comma operator

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

It's the comma operator which evaluates each operand and returns the rightmost. In this case it evaluated 0, then 615*(double) 61 and resulted in a value of that product.

Mark B
  • 95,107
  • 10
  • 109
  • 188