0

I got an interesting program what I can't 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.

Edit:

Okay, so the help with you guys i figured out how it works, and what are the pre and postfix operands, many thanks!

Now i started to print it out how it works, and i found a little weird thing. if i run this

if(f-- && e++ && d++ & c-- || b++)
cout << "f:" << f << " e:" << e << " d:" << d << " c:" << c << " b:" << b << endl;

it gives the following: f:1 e:3 d:1 c:1 b:2 everything is incremented and decremented except the C variable. which is little weird. Anyone any thought while C variable is the only one which is not decremented and why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Thanks for marked as duplicate, but i dont get it why my question is similar to this "if( functionA() && functionB() && functionC() ) cout<<"Hello world";" in my question theres no functions (or i dont know it these are handled as functions in this case? ) Anyway, i figured out the f-- && e++ part would be true (2-1 = 1 / true ) and (2+1 = 3 / true) but the next && how handle it it will handle as f-- && e++ && d++ or just f-- && d++ or e++ && d++ or true && d++? – Studiestheworld Aug 23 '15 at 09:09
  • It doesn't really matter what are the operands. Also, `operator++` and `operator--` could be considered functions, and they also can be overloaded (not for built-in types). You should never write code like this: it's unreadable, unmaintainable and a short way to undefined behavior. See also: [Undefined behavior and sequence points](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – Ivan Aksamentov - Drop Aug 23 '15 at 09:17
  • @Studiestheworld - The answer is still only of academic interest. If you *ever* try to do this is real production code, you will own the code review team free beers for the rest of the year. – Bo Persson Aug 23 '15 at 09:29
  • Thanks for your comment, well its a uni project and its hard to understand for me, its for understand how evaulate. So far i started testing one-by-one what happens if i use if(f-- && e++) it gives back true, but if i use if(f-- && d++) it gives back false which is confuses me, because true and true (f = 1 and d = 1) = true. – Studiestheworld Aug 23 '15 at 09:33
  • @Studiestheworld - Another thing would then be to study the difference between `f--` and `--f`. Specifically if it returns the value before or after the decrement (hint, hint!). – Bo Persson Aug 23 '15 at 09:44
  • @Bo Persson oh my god, thanks! Your right, its checking the variable before decrementing. – Studiestheworld Aug 23 '15 at 09:50

0 Answers0