Compiler control directive allows the compiler to compile a corresponding code on some condition. These can specifically use to control the compiler to run/escape a program.
Syntax
#ifdef:- we use this pre-processor to check if the provided macro exists before including the subsequent code in the compilation process.
#ifndef:- it checks macro substitution preprocessor, if the provided macro does not exist then the corresponding code will execute in the compilation process.
#else:- it is used to write an alternative part of a code
#endif:- it checks whether the given token has been defined earlier in the or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement.
#if:- it is similar with #ifdef where #ifdef check the macro definition exists or not but #if check the value of define function.
Let's see here to understand the code in more depth
Initially we have define x with 100 and we check x is present in program or not in following lines, if not we have define x with now value I.e. 200, but we have already define x with 100 so, it will execute 100 when program will run.
#define x 100
#ifndef x // checking x is present or not
#define x 200 // if not defining x
#endif // closing if statement.
In the following program, we have defined a y with 100 and include macro. c in the next line we have to check macro y is present or not, wherein next to that we have undefined y if it is present in the program and we end up with that.
#deine y 100
#ifdef y // checking y is present or not
#undef y // undefine y
#endif // closing if statement
QUESTION:- write a program to check either a macro variable Is define or not in the program, if yes print a message.
Output
X is defined
Let's discuss another program here to undefine a code.
output
The value of x is undefined.
0 Comments