This blog is under construction

Sunday, 12 January 2014

#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.



2 comments:

  1. To get more exampls of "c programming" visit http://www.cprogramming-bd.com/c_page1.aspx#c%20programming

    ReplyDelete