i have a question about relational operators are they always give correct results ? because if we run this line of code it will result 1 instead of 0
cout<<(ULLONG_MAX==-1);
and if we keep subtracting 1 of both sides it will keep resulting 1
so this could give wrong results in our programs
what is the solution to it?
In this comparison
You have one
unsigned long longand oneint. Before the values are being compared, the operands undergo integral promotion in which theintis promoted tounsigned long long, which is like doingstatic_cast<unsigned long long>(-1)so that the two operands have a common type.Now, an
unsigned long longcan't hold the value-1and it will then "wrap around" to become the largest possibleunsigned long long- which is exactly the valueULLONG_MAXholds.The comparison is therefore
trueand printingtruewill show1(unlessstd::boolalphais in effect).