0

What does the commands c=a,b and d=(a,b) mean in C++?

At first, I thought they were wrong syntaxes but it turned out that the compiler could understand that

19th Nov 2020, 4:25 PM
Hữu Đức Phạm
Hữu Đức Phạm - avatar
8 Respuestas
+ 3
If you use it like that, the comma actually acts as an operator. It first evalutes its left operand, discards it, and then evaluates and returns its right operand. Its precedence is very low, lower than that of the assignment operator. Therefore, in the first example, 'c' would first be assigned the value of 'a', the result would be discarded, and 'b' would be evaluated and returned (with no effect though). However, in the second example, the parentheses change the order of evaluation, so 'c' would be assigned the value of 'b' after the comma operator returns it. In case you want to know more, try looking it up, for example here: https://en.cppreference.com/w/cpp/language/operator_other
19th Nov 2020, 4:39 PM
Shadow
Shadow - avatar
+ 2
Isn't the output 40 50 in compliance with what I said initially? The program does exactly what I said would happen: In the first case, 'c' is assigned the value of 'a', but the value of 'b' in the latter case. Sorry, but I still don't know what exactly you are getting at.
19th Nov 2020, 6:48 PM
Shadow
Shadow - avatar
+ 2
I see, thanks for clarifying. I was talking about the result of the assignment. The assignment operator returns a reference to its left operand to allow for chained assignent calls, so in this case, "c = a" would return a reference to 'c' which would then be discarded before 'b' is evaluated. It doesn't mean the value of 'c' is discarded or anything.
19th Nov 2020, 7:28 PM
Shadow
Shadow - avatar
+ 1
rodwynnejones According to the operator precedence table, yes: https://en.cppreference.com/w/cpp/language/operator_precedence But I can mess up of course. What would you say is incorrect about it?
19th Nov 2020, 6:19 PM
Shadow
Shadow - avatar
+ 1
your comment,,,,"Therefore, in the first example, 'c' would first be assigned the value of 'a', the result would be discarded, and 'b' would be evaluated and returned (with no effect though)" ..........."The result would be discarded" <----- this is what I'm getting at or am I misunderstanding what your implying.. edit...Been pondering your initial comment and.....yes...i have mis-interpreted your post...forgive me.
19th Nov 2020, 7:10 PM
rodwynnejones
rodwynnejones - avatar
0
@Shadow Are you sure with regards to your comment on the first one...c = a, b?
19th Nov 2020, 5:58 PM
rodwynnejones
rodwynnejones - avatar
0
int a = 40; int b = 50; int c; c = a, b; //the "=" has a higher precedence than "," so "c" is assigned the value of "a" printf("%d\n", c); // and the "b" is discarded. c = (a, b); //...but here, the "a" is discarded and"c" is assigned the value of "b". printf("%d", c); // although both give complier warnings or have i misunderstood your initial comment. edit just to clarify...your comment "the result would be discarded" is what i'm questioning.
19th Nov 2020, 6:39 PM
rodwynnejones
rodwynnejones - avatar