c ifndef

c ifndef

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 directive #define , then statements are compile between #ifdef and #endif .If micro_name not be defined or undefined using #undef directive then these statements will not compiled

syntax of #ifndef

#ifndef macro_name
……….
……….

#endif

if macro_name has been defined using the #define and undefined using #undef, then these statements are not compiled, if the macro_name has not been defined using the #define and if we undefined using #undef , then the statements between #ifndef and #endif will 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 defined, so the statements between directive #ifdef and directive #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

/* program to understand the use of #ifndef directive */
#include<stdio.h>
int
main()
{
int a=18,b=3;
#ifndef MAX
printf("MAX is not defined \n" );
a--;
b--;
#endif
printf("a=%d and b=%d\n", a,b);
printf("program complete \n" );
return 0;
}