C program to define, undefine and redefine a macro – #define, #undef

Write a C program to define, undefine and redefine a macro (compile time constant). How to define, remove and redefine a macro in C programming. How to define, remove and redefine a compile time constant (macro) in C programming.

In this post I will explain how to define a compile time constant or macro. Later we will learn how to undefine or remove a macro and finally redefine a defined compile time constant (macro) in C programming.

Required knowledge

Basic C programming, Macros

What is constant and why we need it?

Constant variables are such variables whose value once defined cannot be modified later in program. Values which we don’t want to change throughout the program we declare as constant. For example – value of PI.

To understand need of a constant let us consider below example to calculate area of circle.

#include <stdio.h>

int main()
{
    float radius, pi, area;
    pi = 3.14159;
    radius = 12;
    area = pi * radius * radius;

    printf("Area = %f", area);

    return 0;
}

In above program we declared and used pi as a normal variable. We can easily modify the value of pi anywhere in program. In bigger programs you may accidentally alter the value of pi which would be a nightmare for expressions dependent on pi. Since PI is a mathematical constant whose value is fixed.

Declaring constants restrict programmers to alter its value. The compiler looks for change in the constant variable and report errors if found.

Let us improve our program using a compile time constant (macro).

How to declare a macro?

You define a macro using #define preprocessor directive. We use #define to declare any compile time constant/macro.

Syntax to declare macro:

#define MACRO_NAME consant_value

Where MACRO_NAME is name of the constant to define and constant_value is value of the constant. 

Note: Macro definitions do not terminate with semicolon. Instead they terminate using new line.

Example to declare a macro:

#define PI 3.14159

How to undefine a macro?

C programming allows deletion of macros. You can undefine or remove a macro using #undef preprocessor directive. 

Syntax to undefine a macro:

#undef MACRO_NAME

Where MACRO_NAME is the macro to undefine.

Example to undefine a macro:

#undef PI

How to redefine a macro?

Redefining a macro, you may actually never require this in real life programming. Also this should not be practiced. But there may happen situations where you need to redefine an already defined macro with different value.

C does not support any additional directive to redefine an existing macro. You can define the same macro any number of times. However. doing so will populate your compiler warning stack. Hence it is always advisable to first undefine an existing macro then define it again.

Example: 

// Define a macro
#define PI 3.14

// Undefine before redefining
#ifdef PI
#undef PI
#endif

// Redefine PI
#define PI 3.14159

Program to define, undefine and redefine a macro

/**
 * C program to define, undefine and redefine a macro
 */

#include <stdio.h>

// Define PI
#define PI 3.14

int main()
{
    printf("Value of PI: %f\n", PI);

    // Undefine PI
    #ifdef PI
    #undef PI
    #endif

    // Redefine value of PI
    #define PI 3.14159

    printf("Value of PI after redefinition: %f", PI);
    
    return 0;
}

Output

Value of PI: 3.140000
Value of PI after redefinition: 3.141590

Happy coding 😉