You could define some macros as follows, which would neaten things up....
#ifdef UNC
#define SetDx(f, v) (f)->dx = (v)
#define PRINT_UNC(x) printf x
#else
#define SetDx(f, v)
#define PRINT_UNC(x)
#endif
Then write your code as
int force(Foo* m, Foo* a, Foo* f){
f->x = m->x * a->x;
SetDx(f, (f->x)*(m->dx/m->x + a->dx/a->x));
return 0;
}
int main(){
Foo m; m.x = 3.0;
Foo a; a.x = 2.0;
SetDx(&m, 0.3);
SetDx(&a, 0.2);
Foo f;
force(&m,&a,&f);
printf(" f is %f \n", f.x);
PRINT_UNC(("df is %f \n", f.dx));
return 0;
}
This way you can minimise the number of #ifdef block in your code, which make things difficult to read. When you turn of the main define, the SetDx
and PRINT_UNC
macros will do nothing. (Note the need to double brackets in the PRINT_UNC
macro).
Just a quick question about the part of your question that says:
...slowing down the compiler by processing all these unnecessary items once UNC is turned off...
Are you worried about the pre-processing speed? I though this sounded like you might be worrying that the compiler was taking time compiling the ifdef as a kind of switch... if that's the case, don't worry, the pre-processor kills the undefined blocks so the compiler never sees them. If you're worried about pre-processing speed, there's not a lot you can do I don't think, but I wouldn't have thought the pre-processing overhead would be significant?