c ifdef
The directives #ifdef and #ifndef provide an alternative short form for the combining #if with defined operator.
let take examples of comparasions
#if defined(micro_name) is equivalent to #ifdef macro_name
syntax of #ifdef
mircro_name
…………
…………
…………
|
if the macro_name has been defined with the #define directive, then the statements between the #ifdef and #endif will be compiled only. If there is micro_name not be defined or undefined using #undef directive then these statements are not compiled
syntax of #ifndef
if macro_name has been defined, using the #define or undefined using #undef then these statements are not compiled
if the macro_name has not been defined using the #define or undefined using #undef, then the statements between #ifndef and #endif are compiled let us take example to understand
/* program to understand the use of #ifdef directive */
COUNT
main()
{
a=30,b=3;
COUNT
printf("COUNT is defined \n");
a++;
b--;
printf("a=%d,b=%d \n",a,b);
printf("program complete \n");
0;
}
|
output:
COUNT is defined
a=31,b=2
program complete
|
So in the above example FLAG has been defind, so the statements between #ifdef and #endif are compiled. if we delete the defination of marcro COUNT from the program, then these statements will not be compiled and the output of the program would be
output:
a=31,b=2
program complete
|