1

I am using VS2005 compiler and I am expecting following code to give compilation error.

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

value of a after assignment is 3. As per my understanding it should give compilation error.

I would be happy to know if there is any valid reason behind this.

Vijay
  • 2,021
  • 4
  • 24
  • 33
  • Why in the world would this be a compilation error? Unless you mean the semicolon you forgot at the end of the second line? – Cody Gray - on strike Dec 29 '11 at 11:53
  • First of all, *why* do you want this to be a compilation error? – user703016 Dec 29 '11 at 11:53
  • It would be more helpful to post a more complete sample, since your code as it is **does** give a compilation error - it's missing a trailing semicolon and you can't assign values at that scope. Does the following reflect what you're expecting? int main() { int a=1, b= 2, c=3; a = (b,c); } – bacar Dec 29 '11 at 11:55
  • As per my understanding, if statement is not doing anything or causing problems then may be compiler should giver error. Here, variable b is not at all in used and anyone reading code can get confused. Its its not used at all then it should give error, as per my understanding. – Vijay Dec 29 '11 at 12:11
  • @Vijay: The compiler can't give an error, since the code is valid. You might be able to make it give a warning; certainly GCC will warn about this if you specify `-Wall` or `-Wunused` (and you can make this an error if you like by also specifying `-Werror`). – Mike Seymour Dec 29 '11 at 13:11
  • Vijay: useful update but it still doesn't compile. It's easy to turn this into a very small but complete file sample (put your code inside a function) so you may as well do so... – bacar Dec 29 '11 at 15:26

5 Answers5

6

The only error in your code is that you forgot the semicolon at the end of the second line, but I assume that was a typographical error in composing your question, rather than the actual thrust of your question.

I see no reason why the code you've shown should produce a compilation error. It compiles just fine for me, and the value of a is, in fact, 3.

See for yourself: the following code returns 3:

int main()
{
    int a=1, b= 2, c=3;
    a = (b,c);

    return a;
}

The trick is your use of the comma operator, which evaluates its first operand and then discards the result, and then evaluates the second operand and returns its value.

However, as Charles Bailey notes, you have to wrap the code shown in the question inside of a function, otherwise you will get compilation errors in any compiler. C++ doesn't allow assignment statements outside of functions.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
5

You are using the comma operator in C++, it is not commonly used. This works as follows

<expression1>, <expression2>

It evaluates <expression1> and discards the results and then evaluates <expression2> and takes the result of that is returned as the value of the whole expression.

Thirler
  • 20,239
  • 14
  • 63
  • 92
  • why we need such a thing? just curious. – Vijay Dec 29 '11 at 12:09
  • It is sometimes used to do multiple things in the header of for-loops and a few other cases. In general most people don't know it even exists and C++ could well do without. – Thirler Dec 29 '11 at 12:23
1

There is no error in this piece of code. Why do you think there should be a compilation error? All here is is a comma operator which evaluates all its parameters, but returns the rightmost one: in this case 3.

sim642
  • 751
  • 7
  • 14
1

To quote http://en.wikipedia.org/wiki/Comma_operator:

"In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point."

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
friendzis
  • 799
  • 6
  • 17
1

I've just tested this on VS2005 and I get compilation errors as expected.

Compiling...
main.cpp
d:\dev\work\comptest\main.cpp(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\dev\work\comptest\main.cpp(2) : error C2374: 'a' : redefinition; multiple initialization
        d:\dev\work\comptest\main.cpp(1) : see declaration of 'a'

The declaration line is fine, but as expected the assignment statement is not valid outside of a function. The compiler appears to interpret it as an attempt to re-initialize a with a default int type, neither of which is legal.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • I think this answer misses the point - the question makes it reasonably clear that they are surprised that it *does* compile and that `a` gets a value of 3 - we can reasonably conclude that they've pasted an incomplete snippet? – bacar Dec 29 '11 at 12:08
  • 1
    @bacar: I stand by my answer, the posted code doesn't compile (as it shouldn't, as the poster expects - even if he doesn't say what error he was expecting or why). If it does compile for the poster then he has done something wrong. I have gone to the trouble of testing his code with his compiler so I hope this will help him find the discrepancy. – CB Bailey Dec 29 '11 at 12:09