#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

textcolor in c

wherex in c

traffic light program in c, traffic light simulation