I am a bit confused on the type of expression we can use with the #IF preprocessor in the C language. I tried the following code, and it isn’t working. Please explain and provide examples for expressions that can be used with the preprocessor.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int c=1;
#if c==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif
int main()
{
int a = 0, b;
printf("a = %d\n", a);
b = check(a);
printf("a = %d %d\n", a, TABLE_SIZE);
system("PAUSE");
return 0;
}
The preprocessor cannot use variables from the C program in expressions – it can only act on preprocessor macros. So when you try to use
cin the preprocessor you don’t get what you might expect.However, you also don’t get an error because when the preprocessor tries to evaluate an identifier that isn’t defined as a macro, it treats the identifier as having a value of zero.
So when you hit this snippet:
The
cused by the preprocessor has nothing to do with the variablecfrom the C program. The preprocessor looks to see if there’s a macro defined forc. Since there isn’t, it evaluates the following expression:which is false of course.
Since you don’t appear to use the variable
cin your program, you can do the following to get behavior in line with what you’re trying:(Note that I also made the macro name uppercase in keeping with convention for macro names.)