c ifdef

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

#ifdef mircro_name
…………
…………
…………

#endif

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

#ifndef macro_name
……….
……….

#endif

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 */
#include<stdio.h>
#define
COUNT
int main()
{
int a=30,b=3;
#ifdef COUNT
printf("COUNT is defined \n");
a++;
b--;
#endif
printf("a=%d,b=%d \n",a,b);
printf("program complete \n");
return 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