M
Michael Wojcik
Undefined behaviour if fopen() fails. Either drop the unnecessary
fclose call or execute it as part of the the if statement.
Thanks, Dan. That's what I get for not proofing before I post...
It doesn't matter, as long as the program's output is not specified by
the C language. A compiler could optimise a correct version of your
program to:
int main(void)
{
return 0;
}
without having its conformance affected (the standard doesn't guarantee
the success of *any* fopen call).
That final parenthetical clause is certainly true, and after glancing
back over section 4 of 9899-1990 I believe you're right. A conforming
implementation could optimize away the fopen call even if that would
change the program's output, since such an implementation is only
required to "accept" strictly conforming programs, and to not provide
any extensions which would alter the behavior of strictly conforming
programs.
People need to understand that certain aspects of an implementation's
behaviour are not controlled by the C language definition (consider the
same program compiled and executed under Windows).
I think the comment regarding Windows is moot. My program (after your
correction) was intended only as an example for one implementation
(which for the sake of argument I assumed was conforming; at any rate,
the example didn't rely on any non-conforming behavior by it). The
fact that on other implementations it wouldn't demonstrate a side
effect of an otherwise-inconsequential fopen is entirely beside the
point.
In short, while it might be possible to have a (not strictly)
conforming program detect the effects of optimization in a given
implementation, that apparently does not in itself alter the
conformance of that implementation.