Skip to main content

#ifdef, #ifndef and #endif directives in C

#ifdef
If the named macro is defined, then the code between #ifdef and #endif will be compiled.

#ifndef
If the named macro is not defined, then the code between #ifndef and #endif will be compiled.


#ifdef, #ifndef and #endif example in C
 
#include <stdio.h>
#define NAME "Raj"

int main() {

#ifdef NAME
printf("#ifdef: Value of NAME is %s\n", NAME);
#endif

#ifndef VAL
printf("#ifndef: macro VAL is not defined\n");
#endif
return 0;
}


  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  #ifdef: Value of NAME is Raj
  #ifndef: macro VAL is not defined


In the above program, macro NAME is defined.  So, the code between #ifdef and #endif is executed. Similarly, the code between #ifndef and #endif is executed as the macro VAL is not defined.



Comments

Popular posts from this blog

restorecrtmode example in c

Header file:     graphics.h Synopsis:        void restorecrtmode();       Description:       restorecrtmode() restores screen mode to text mode. restorecrtmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...