itachi
itachi

Reputation: 302

Increment twice in same std::cout statement

I'm not sure why the out put is reversed in second case. Its probably straightforward but I don't think I can spend more time thinking about this. Any help/suggestions are appreciated. Note compiler is VS2012

double array[] = {10.0,20.0,30.0,40.0,50,60.0,70,80,90,100.0};
double *it = array;
int size = 10;
while(i<size) 
{               
    std::cout << "First : " <<  *it;
    it++;
    std::cout << "   Second : " << *it << std::endl; 
    it++;
    i++;        
}
//output
//First : 10.0  Second : 20.0
//First : 30.0  Second : 40.0
//...and so on

/*
while(i<size) 
{
std::cout << std::setprecision(15) << "First : " <<  *it++ << "   Second : " << *it++ <<  std::endl;
i++;
}
//Output is reversed in this case
//First : 20.0  Second : 10.0
//First : 40.0  Second : 30.0
//...and so on

*/

Upvotes: 0

Views: 211

Answers (1)

Alexey
Alexey

Reputation: 144

This happens because the assignments are made in undefined order, most probably that is the reason why it shows the values reversed. Source: http://en.cppreference.com/w/cpp/language/eval_order

Upvotes: 1

Related Questions