#undef directive in C

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 20 minutes | Coding time: 5 minutes

#undef is a preprocessor directive to remove an existing macro. It is useful as the only way to update an existing macro is to delete it (using undef) and create a new macro (using define). It is, also, used tom make sure we are calling functions instead of macros.

Syntax:

#undef MACRO_NAME

Example:

#define INTEGER 1
#undef INTEGER

In the above code, we are defining a macro named INTEGER with value 1 and then, undefining/ removing the macro INTEGER.

Example

Following is the complete example of an simple example of undefining macro.

#include <stdio.h>

#define INTEGER 1

int main() {
    printf("%d", INTEGER);
    #undef INTEGER
    //printf("%d", INTEGER); // will give compile time error
	return 0;
}

Output:

1

If we keep the second printf statement, we will get a compilation error as:

opengenus.c: In function 'main':
opengenus.c:8:18: error: 'INTEGER' undeclared (first use in this function)
     printf("%d", INTEGER); // will give compile time error
                  ^
opengenus.c:8:18: note: each undeclared identifier is reported only once for each function it appears in

Example: undef a non-existant macro

In this example, we will try to undef a macro which has not been declared. This will not give any compilation error and this does not cause any certain issue.

#include <stdio.h>

#undef INTEGER

int main() {
    printf("%d", 1);
	return 0;
}

Output (no compilation error):

1

Updating a macro

In this example, we have updated the value of macro INTEGER from 1 to 2 using ifdef and undef.

#include <stdio.h>

#define INTEGER 1

int main() {
    printf("%d\n", INTEGER);
    #ifdef INTEGER
    #undef INTEGER
    #define INTEGER 2 
    #endif
    printf("%d\n", INTEGER); 
	return 0;
}

Output:

1
2

In this example, we have updated the value of macro INTEGER from 1 to 2 using ifndef and undef.

#include <stdio.h>

#define INTEGER 1

int main() {
    printf("%d\n", INTEGER);
    #undef INTEGER
    #ifndef INTEGER
    #define INTEGER 2 
    #endif
    printf("%d\n", INTEGER); 
	return 0;
}

Output:

1
2

undef to use actual functions

To avoid situations where macro name is same as a function which may result in wrong behaviour, we must undef the function name to ensure that we are calling a function and not a macro.

To illustrate the problem, we will see a C code example where a macro is defined same as a function name which results in different behaviour. Note that the macro can be defined in any header file so debugging this can be difficult.

#include <stdio.h>
#include <math.h>

#define pow(x, y) (x+y)

int main(void)
{
   int c = pow(3, 2);
   printf("%d\n", c);
   return(0);
}

Output:

5

The problem is the pow function is supposed to find the power but it is infact calculating the addition due to the macro definition. To avoid such situations, we must undef the function name before using it.

#include <stdio.h>
#include <math.h>

#define pow(x, y) (x+y)

int main(void)
{
   #undef pow
   int c = pow(3, 2);
   printf("%d\n", c);
   return(0);
}

Output:

9

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.