comma-operator

Comma operator returns first value instead of second in argument list?

强颜欢笑 提交于 2019-12-18 11:44:11
问题 MDN claims that: The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand . However, when I tried running <script> alert(1, 2); </script> , it shows a "1" instead of a "2". Am I misunderstanding something? 回答1: In the context of a function call, the comma is used to separate parameters from each other. So what you're doing is passing a second parameter to alert() which gets silently ignored. What you want is possible this way: alert((1

How do I put two increment statements in a C++ 'for' loop?

試著忘記壹切 提交于 2019-12-17 06:25:57
问题 I would like to increment two variables in a for -loop condition instead of one. So something like: for (int i = 0; i != 5; ++i and ++j) do_something(i, j); What is the syntax for this? 回答1: A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus: for(int i = 0; i != 5; ++i,++j) do_something(i,j); But is it really a comma operator? Now having wrote that, a commenter suggested it was actually some special syntactic sugar in the for

Javascript comma operator

∥☆過路亽.° 提交于 2019-12-13 20:44:54
问题 When combining assignment with comma (something that you shouldn't do, probably), how does javascript determine which value is assigned? Consider these two snippets: function nl(x) { document.write(x + "<br>"); } var i = 0; nl(i+=1, i+=1, i+=1, i+=1); nl(i); And: function nl(x) { document.write(x + "<br>"); } var i = 0; nl((i+=1, i+=1, i+=1, i+=1)); nl(i); The first outputs 1 4 while the second outputs 4 4 What are the parentheses doing here? 回答1: I was confusing two things, here. The first

Comma operator and void expression

半城伤御伤魂 提交于 2019-12-10 02:58:50
问题 I came across this code snippet 1 int return_printChar1() { // code // oops! no return statement } int return_printChar2() { // code return printf("Return"); } int main() { int i; // some more code i = return_printChar2(); if((return_printChar1(),i)) { printf ("Gotcha"); } } 1: This is not a real life example. My question is " Is the behaviour of the code snippet well defined in C and C++? " My take: In C the behaviour is well defined because 6.5.17 says The left operand of a comma operator

Behavior of comma operator in C [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-08 10:08:58
问题 This question already has answers here : What does i = (i, ++i, 1) + 1; do? (7 answers) Closed 2 years ago . If I write code using comma operator like this: int i; i = 5, i++, i++; does it invoke undefined behavior? 回答1: No. It will not invoke undefined behavior as there is a sequence point between evaluation of left and right operands of comma operators. = has higher precedence than that of , operator and therefore 5 will bind to = as (i = 5), i++, i++; Since operands of comma operator

Comma operator and void expression

安稳与你 提交于 2019-12-05 03:08:47
I came across this code snippet 1 int return_printChar1() { // code // oops! no return statement } int return_printChar2() { // code return printf("Return"); } int main() { int i; // some more code i = return_printChar2(); if((return_printChar1(),i)) { printf ("Gotcha"); } } 1: This is not a real life example. My question is " Is the behaviour of the code snippet well defined in C and C++? " My take: In C the behaviour is well defined because 6.5.17 says The left operand of a comma operator is evaluated as a void expression ; there is a sequence point after its evaluation In C++03 the

Different behaviour of comma operator in C++ with return?

浪尽此生 提交于 2019-12-02 18:40:24
This (note the comma operator ): #include <iostream> int main() { int x; x = 2, 3; std::cout << x << "\n"; return 0; } outputs 2 . However, if you use return with the comma operator, this: #include <iostream> int f() { return 2, 3; } int main() { int x; x = f(); std::cout << x << "\n"; return 0; } outputs 3 . Why is the comma operator behaving differently with return ? According to the Operator Precedence , comma operator has lower precedence than operator= , so x = 2,3; is equivalent to (x = 2),3; . (Operator precedence determines how operator will be bound to its arguments, tighter or looser

Move constructor suppressed by comma operator

て烟熏妆下的殇ゞ 提交于 2019-12-01 15:14:31
This program: #include <iostream> struct T { T() {} T(const T &) { std::cout << "copy constructor "; } T(T &&) { std::cout << "move constructor "; } }; int main() { ([](T t) -> T { return t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), std::move(t); })({}); std::cout << '\n'; } when compiled by gcc-4.7.1 outputs ( link ): move constructor copy constructor move constructor Why does the comma operator have this effect? The standard says: 5.18 Comma operator [expr.comma] 1 - [...] The type and value of the result are the

In which order are the increment expressions on the right evaluated in the assignment statement? Is it undefined?

…衆ロ難τιáo~ 提交于 2019-12-01 13:29:18
I recently learnt about undefined behaviour in C, but this particular code was used in a site as an example for 'comma as an operator', and while I understand how y = x++ in line 2, I dont understand in what order the sub expressions in line 2 are evaluated. I think it is undefined behaviour, but I'm not sure,because the site didn't mention anything as such. int main() { int x = 10, y; y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++); printf("y = %d\n", y); printf("x = %d\n", x); return 0; } Output: x = 11 x = 12 y = 12 x = 13 It is not undefined behaviour. You first increase

In which order are the increment expressions on the right evaluated in the assignment statement? Is it undefined?

北城以北 提交于 2019-12-01 12:32:49
问题 I recently learnt about undefined behaviour in C, but this particular code was used in a site as an example for 'comma as an operator', and while I understand how y = x++ in line 2, I dont understand in what order the sub expressions in line 2 are evaluated. I think it is undefined behaviour, but I'm not sure,because the site didn't mention anything as such. int main() { int x = 10, y; y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++); printf("y = %d\n", y); printf("x = %d\n",