This blog is under construction

Sunday, 3 June 2012

#undef directive in C

#undef directive is used to undefine a macro that has been defined earlier using #define. It is used if any macro is not needed or if the macro needs to redefined.

Example:
     #undef SAMPLE

The above statement would remove the definition for the macro named SAMPLE.

#undef directive example in C

  #include <stdio.h>
  #define NUM 10

  int main() {
        int a = NUM;
        printf("Value of a: %d\n", a);
        #undef NUM   // undefining macro NUM
        #ifdef NUM
             printf("NUM macro defined\n");  // this part is ignored as NUM is undefined
        #else
             printf("Undefine macro NUM\n");
        #endif
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/cpgms/preprocessor$ ./a.out
  Value of a: 10
  Undefine macro NUM



No comments:

Post a Comment