dimanche 23 août 2015

evaulation order in C++ in a variable [duplicate]

This question already has an answer here:

So i got an interesting program what i cant understand completely.

#include <iostream>
#include <typeinfo> //added by me to check 'h' variable type
using namespace std;

int main()
{

    int a = -1, b =1, c = 1, d = 0, e = 2, f = 2, g = 0;
    int  h =  f-- && e++ && d++ && c--; || b++ || a++; //interesting part
    cout << "f:" << f << << " e:" << e << " d:" << d << " c:" << c << " b:" << d 
    << " a:" << a << " g:" << g << " h:" << h <<endl;
    cout << "h variable type :" << typeid(h).name() << endl;
    return 0;
    } 

so the 'h' variable is the problem, its adding and substracting while reach the c-- part (c-- never happens) and the a++ never happens too just like c--. Why is that? If im right its something like: h = f-- (true) and e++ (true) and d++ (true) and c-- (which is false (if c=0) else happens) or b++ which is (true) or a++ (which is also true). the 'h' variable type is int. but its looks like a boolean to me.

Aucun commentaire:

Enregistrer un commentaire