I have two examples here that do exactly the same thing. The gcc compiler apparently produces the same assembly code for both variants:
Example 1 with if else:
#include <stdio.h>
#include <stdlib.h>
#include <checkcpu.h>
int main(int argc, char *argv[])
{
if (!check386()) {
printf("This program does require a 386 or better.n");
exit(EXIT_SUCCESS);
} else {
innerMain();
}
return 0;
}
Example 2 with only if without else. The code that is executed in the Else block is simply appended after the if.:
#include <stdio.h>
#include <stdlib.h>
#include <checkcpu.h>
int main(int argc, char *argv[])
{
if (!check386()) {
printf("This program does require a 386 or better.n");
exit(EXIT_SUCCESS);
}
innerMain();
return 0;
}
Are there reasons to prefer one variant over the other?
For example, are there older compilers where this makes a difference?
Which one is easier to read and therefore better design?
1