+ 2
How to work this program? Explain this program.
#include <iostream> using namespace std; int main() { int i=1,j=2,k=3; int w; w=(i,j,k); cout <<w; int r; {r=i,j,k;} cout<<r; int q; q=i,j,k; cout<<q; return 0; }
2 Answers
+ 8
https://en.wikipedia.org/wiki/Comma_operator
You may also take a look at C++ operator precedence.
http://en.cppreference.com/w/cpp/language/operator_precedence
As you can see, comma is at the bottom of the table (least precedence). Hence, when we do:
w=(i,j,k);
(i,j,k) is evaluated first due to the parenthesis.
As for {r=i,j,k;} and q=i,j,k;
The assignment operator = is evaluated prior to the comma operator.
+ 1
output :311