more questions about efficient C code

Martin Minow minow at decvax.UUCP
Wed Jun 26 12:14:32 AEST 1985


The question was asked whether the common C idiom
	if ((fp = fopen("foo.bar", "r")) == NULL) { ...
really is more efficient than
	fp = fopen("foo.bar", "r");
	if (fp == NULL) { ...

In response, Joe Durnavich makes the good point that the saving
of one measly move instruction (which a good compiler would
optimize out anyway) isn't worth the effort.

In response, I would again point out that this is a C idiom --
one of the habits a programmer picks up for better or worse.
In defense, I would note that writing this as a single statement
ties (visually) the if condition closer to the fopen() result that
is being tested than would be the case were one to write two
statements.

Martin Minow
decvax!minow



More information about the Comp.lang.c mailing list