I am writing a big code and I don’t want it all to be in my main.c so I wrote a .inc file that has IF-ELSE statement with function and I was wondering can it be written like this:
#if var==1
process(int a)
{
printf("Result is: %d",2*a);
}
#else
process(int a)
{
printf("Result is: %d",10*a);
}
#endif
I tried to compile it but it gives me errors or in best case it just goes on the first function process without checking the var variable (it is set to 0).
The preprocessor doesn’t “know” the value of any variable, because it does its work even before compilation, not at runtime.
In the condition of a preprocessor
#ifyou can only evaluate#define‘d symbols and constant expressions.The particular example you are showing can be simply converted to: