So for example is there a difference in optimization in these these two ways of checking.
What the compiler does, whether or not it will optimize, is up to the compiler to decide as long as the observable behaviour does not change (how long it takes for your code to run does not count as observable behaviour). |
In this case, however, the "short-circuiting" of the logical operators is
not just an optional compiler-optimization, but
is an observable behavior that is guaranteed by the standard (the second operand may have some side side-effects, which only occur if it is actually evaluated; and we must be able to rely on that).
So, the "two ways of checking" are two different ways of writing down the
exactly same logic/behavior.
________
For example, in the following code (which actually
is safe) we must 100% rely on the fact that the second operand will
never be evaluated if the first one was
false, or otherwise our program may crash:
1 2 3 4 5
|
FILE *file = fopen("myfile.txt","r");
if ((file != NULL) && (fread(buffer, 42, 1, file) == 1))
{
/* ... */
}
|