Alex Rivera | Logout

question about ? and : in c++

Asked 2010-05-09T11:51:07.900
9

Why this statement :

int a = 7, b = 8, c = 0;
c = b>a?a>b?a++:b++:a++?b++:a--;
cout << c;

is not equal to :

int a = 7, b = 8, c = 0;
c = (b>a?(a>b?a++:b++):a++)?b++:a--;
cout << c;

and is equal to :

int a = 7, b = 8, c = 0;
c = b>a?(a>b?a++:b++):(a++?b++:a--);
cout << c;

Please give me some reason. Why ?

Edit
Report

1 Answer

15

Because ? : is right-to-left associative. It's defined like that in the language.

answered 2010-05-09T11:58:29.457

Your Answer