C #if and #endif preprocessor directive
An expression which is followed by the #if is evaluted, if result is non zero then the statements between #if and #endif are compiled, otherwise thay are skipped, this syntax is
constant_expression
…………
statements
……….
……….
|
The constant expression should be an intergral expression and this should not contain sizeof operator,enum,cast operator or any keywords or variables.
it can contains only arithmetic,logical,relational operators.
/* program to understand the use of #if directive */
COUNT 10
main()
{
a=6,b=7;
COUNT<=12
printf("The value of COUNT is less than or equal to 12 ");
a=a+b;
b=a*b;
printf("values of variables a and b changed");
printf("a=%d and b=%d \n",a,b);
printf("program complete");
0;
}
|
output:
The value of COUNT is less than or equal to 12
values of variables a and b changed
a=13 and b=42
program complete
|
in the above example COUNT is defined with the value 10 . firstly the constant expression COUNT<=12 is evaluated so it is true(non zero) then all statements between #if and #endif are compiled.