I found a bug in code (the if statement should have had “==” instad of “=”) and I have few questions.
Example code:
int i = 5;
if (i = MyFunction()) // MyFunction() returns an int; this is where bug was made
{
// call A()
}
else
{
// call B()
}
From what I gather it should always call A().
1. Is my assumption correct()?
2. Is this case for all/most compilers (any exceptions)?
A()if the assignment is considered true, i.e. non-zero.Some people flip comparisons, writing the constant to the left, to avoid the risk:
but of course this wouldn’t have worked in your case, since it really is an assignment. Most compilers can give you a warning when this code is detected, since it’s so often a cause of bugs.