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 publish I’ll clarify the way to outline a compile time fixed or macro. Later we are going to discover ways to undefine or take away a macro and eventually redefine an outlined compile time fixed (macro) in C programming.
Learn extra the way to outline runtime fixed utilizing const in C program?
Required data
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 don’t wish to change all through this system we declare as fixed. For instance – worth of PI.
To grasp want of a relentless allow us to think about under instance to calculate space of circle.
#embrace <stdio.h>
int predominant()
{
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
anyplace in program. In larger applications it’s possible you’ll unintentionally 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 limit programmers to change its worth. The compiler appears 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.
Be aware: 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 could occur conditions the place that you must redefine an already outlined macro with totally different worth.
C doesn’t help any extra directive to redefine an present macro. You’ll be able to outline the identical macro any variety of instances. Nonetheless. doing so will populate your compiler warning stack. Therefore it’s at all times 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
*/
#embrace <stdio.h>
// Outline PI
#outline PI 3.14
int predominant()
{
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
Completely happy coding 😉