C++ strange program behaviour

 
	cout<<(2,3,4,5,1)<<endl;	

If i type this it just prints the last number...
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int work(int x)
{
	return x;
}
int main()
{
	cout<<work((2,3,4,5,1))<<endl;	
	return 0;
}

Again it types just the last number.....
Explain please?
Last edited on
You are abusing the comma operator.
http://en.wikipedia.org/wiki/Comma_operator
http://en.cppreference.com/w/cpp/language/operator_other

The comma operator evaluates expressions from left to right and its result is the last expression.

1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << (1, 2, 3, "potato") << std::endl;
}
potato

Topic archived. No new replies allowed.