i assumed that the outcome of these two if
statements is ALWAYS the same
BOOL tmp = (expression);
if (expression) {
}
if (tmp) {
}
but as discovered here Why is 0xF00 interpreted as NO, when the dec is not 0
like when checking against a 0xF00
pointer, this might not be always the case...
How does evaluating an if
expression behave differently?
The if
statement is defined in section 6.8.4.1 of the spec. The relevant section is:
2 In both forms, the first substatement is executed if the expression compares unequal to 0.
You will note the lack of BOOL
anywhere in that statement. BOOL
doesn't exist in C; it's just a typedef to signed char
provided by Cocoa. (bool
exists in modern versions of C, but it's a different type, and still not related to the if
statement.)
So if expression
compares unequal to 0, but tmp
compares equal to 0, then they will behave differently. This is the point Tom Harrington was making in the comments.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments