0

Edit: Apologies for posting a duplicate and such a basic question! I thought it was a trickier one than it actually was, and failed to discover the cited article in my original search.

For the language standard experts out there, is the value of an expression like:

(a=1, b=2, c=3)

... defined? From my tests, it appears that all of our compilers evaluate this expression as 3 (GCC, MSVC, Clang).

However, I'm not sure if we should lean on this behavior. I have no intention of writing code like this, but encountered some obscure code which was leaning on this behavior with multiple assignment in a conditional, and was wondering if it was well-defined.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 3
    I suggest reading [comma operator](http://en.wikipedia.org/wiki/Comma_operator). – Jesse Good May 08 '15 at 23:23
  • I see -- that matches the behavior I was seeing. Apologies if I ventured into a complete n00b realm -- I'm not used to writing such expressions in cases where the evaluated result mattered, so I became a bit paranoid when encountering the code hoping for some standard guarantees. –  May 08 '15 at 23:25
  • 2
    And to note, this is actually _somewhat_ common inside for loops, to iterate two things in parallel. – Mooing Duck May 08 '15 at 23:26
  • 2
    @Ike: Np, anyone using C++ will bump into the comma operator sometime :). – Jesse Good May 08 '15 at 23:28
  • 1
    @Ike dont get down on yourself so much for trying to learn and become a better programmer. Sure it was a duplicate, but before this question you may not have even known that the comma can be an operator. Your question was clear and well stated which is better than 90% of questions on this site. – dwcanillas May 09 '15 at 00:28
  • 1
    The situation is unfortunate. You should take it to meta, not complain about it here. Though the picture is nice, but at 7k rep you should understand that we're all nerds here and won't let you keep it in your post, as well as the rant. :) – HolyBlackCat Jan 15 '18 at 11:38

1 Answers1

2

If a, b, and c are all already defined, you are using a comma operator, which would return the value of c = 3, which is 3.

So to answer your question, yes, it is well defined.

dwcanillas
  • 3,562
  • 2
  • 24
  • 33