#ifdef something
int some=0;
#endif
main()
{
int thing = 0;
printf("%d %d\n”, some ,thing);
}
Answer:
Compiler error : undefined symbol some
Explanation:
This is a very simple example for conditional compilation. The
name something is not already known to the compiler making the
declaration
int some = 0;
effectively removed from the source code.
#if something == 0
int some=0;
#endif
main()
{
int thing = 0;
[Read More]
Using ## and # with define.
Stringize(#)
#define message\_for(a, b) \\
printf(#a " and " #b)
int main()
{
message\_for(Carole, Debra);
}
**Token pasting (##)**
#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
**Answer:**
100The preprocessor directives can be redefined anywhere in the program
#include
#define a 10
main()
{
#define a 50
printf("%d”,a);
}
Answer:
50
Explanation:
The preprocessor directives can be redefined anywhere in the program. So
the most recently assigned value will be taken.
Some macros
#define SQR(x) ((x)*(x))
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
#define ISLP(y) ( (y % 400 == 0) || (y %100 != 0 && y%4 == 0) )
#define ISLOWER(a) (a>=97 && a<=127)
#define TOLOWER(a) (a - 32)
Using define without assigning value
#include
#define NO
#define YES
int main()
{
int i = 5,j;
if (i>5) j=YES;
else j=NO;
printf("%d”,j);
}
Output: Expression syntax in function main
Explanation: Because when assigning j with YES or NO we don’t know what is the value of YES or NO.