Sunday, October 2, 2022
HomeC ProgrammingC program to outline, undefine and redefine a macro - #outline, #undef

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


Write a C program to outline, undefine and redefine a macro (compile time fixed). outline, take away and redefine a macro in C programming. outline, take away and redefine a compile time fixed (macro) in C programming.

On this put up I’ll clarify the way to outline a compile time fixed or macro. Later we’ll discover ways to undefine or take away a macro and eventually redefine an outlined compile time fixed (macro) in C programming.

Required information

Fundamental C programming, Macros

What’s fixed and why we’d like it?

Fixed variables are such variables whose worth as soon as outlined can’t be modified later in program. Values which we do not wish to change all through this system we declare as fixed. For instance – worth of PI.

To know want of a relentless allow us to contemplate under instance to calculate space of circle.

#embody <stdio.h>

int foremost()
{
    float radius, pi, space;
    pi = 3.14159;
    radius = 12;
    space = pi * radius * radius;

    printf("Space = %f", space);

    return 0;
}

In above program we declared and used pi as a traditional variable. We will simply modify the worth of pi wherever in program. In larger packages you might by chance alter the worth of pi which might be a nightmare for expressions depending on pi. Since PI is a mathematical fixed whose worth is fastened.

Declaring constants prohibit programmers to change its worth. The compiler appears to be like for change within the fixed variable and report errors if discovered.

Allow us to enhance our program utilizing a compile time fixed (macro).

declare a macro?

You outline a macro utilizing #outline preprocessor directive. We use #outline to declare any compile time fixed/macro.

Syntax to declare macro:

#outline MACRO_NAME consant_value

The place MACRO_NAME is title of the fixed to outline and constant_value is worth of the fixed. 

Notice: Macro definitions don’t terminate with semicolon. As a substitute they terminate utilizing new line.

Instance to declare a macro:

#outline PI 3.14159

undefine a macro?

C programming permits deletion of macros. You’ll be able to undefine or take away a macro utilizing #undef preprocessor directive. 

Syntax to undefine a macro:

#undef MACRO_NAME

The place MACRO_NAME is the macro to undefine.

Instance to undefine a macro:

#undef PI

redefine a macro?

Redefining a macro, you may very well by no means require this in actual life programming. Additionally this shouldn’t be practiced. However there might occur conditions the place it’s essential to redefine an already outlined macro with completely different worth.

C doesn’t help any further directive to redefine an present macro. You’ll be able to outline the identical macro any variety of occasions. Nonetheless. doing so will populate your compiler warning stack. Therefore it’s all the time advisable to first undefine an present macro then outline it once more.

Instance: 

// Outline a macro
#outline PI 3.14

// Undefine earlier than redefining
#ifdef PI
#undef PI
#endif

// Redefine PI
#outline PI 3.14159

Program to outline, undefine and redefine a macro

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

#embody <stdio.h>

// Outline PI
#outline PI 3.14

int foremost()
{
    printf("Worth of PI: %fn", PI);

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

    // Redefine worth of PI
    #outline PI 3.14159

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

Comfortable coding 😉

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments