The #outline Directive for Macros
In C, we outline macros utilizing “#outline” preprocessor directive. Preprocessor directives in C at all times begin with a “#” image. Preprocessor directives are used to perform macro growth, file inclusions, conditional compilation, and different directives.
Naming of Macros
As a conference, macro names are written in higher case. Applications are simpler to learn when it’s potential to inform at a look which names are macros.
Instance:
#embrace
#outline PI 3.1428
int principal()
{
float circumference=0, radius=0;
printf(“Enter your identify: “);
printf(“Enter your identify: “);
scanf(“%f”, &radius);
circumference = 2*PI*radius;
printf(“Circumference of the circle = %fn”,circumference);
return 0;
}
Whereas preprocessing, the part earlier than compilation, all of the macros get changed with the precise values. Bear in mind this occurs earlier than the compilation is completed. So, the dimensions of this system will increase after preprocessing.
Forms of Macros
And, macros needn’t be a worth, they will additionally do a easy perform like work with macros that may have arguments similar to features. At any time when the identify is used, it’s changed by the contents of the macro.
#outline CIRCLEAREA(r) ((3.14)*r*r)
However, why use a macro within the first place? And, why not simply use a variable or a relentless variable. Macros provide a slight profit in pace in comparison with utilizing features as a result of no time is utilized in passing arguments and management, however even that profit is countered by intelligently optimized compilers nowadays.
Macros vs. Constants
Which is preferable – macros or fixed variables?
A variable is sort secure and makes use of scoping guidelines. As a lot as potential, one ought to attempt to be inside the boundaries of sort security. A continuing variable wants a storage location whereas a macro doesn’t want storage. However, the fashionable compilers too exchange constants in order that it’s area optimized. A continuing variable wants initialization whereas a macro doesn’t want any initialization.
So, except you might be engaged on embedded programs or actual time programs the place each byte or each nano second counts (and you might be additionally fearful that the compiler will be unable to optimize away your fixed variable), use of fixed variables is advisable over macros for the sake of security and maintainability. In the long run it’s a private alternative of coding type, however the coding type that over depends on macros appears to be extra buggy.
Conditional Compilation utilizing Macros
Conditional compilation in C depends on a number of preprocessor directives:
- #ifdef
- #ifndef
- #if
- #else
- #elif
- #endif
These directives management whether or not the compiler contains or excludes components of the code based mostly on evaluating situations at compile time. A preferred software of that is whenever you wish to compile various kinds of code for various platforms Win 32, Win64, Linux, and so forth.
Conclusion
Macros in C programming present a strong mechanism for changing code snippets and values all through your program. They improve code readability, scale back redundancy, and enhance maintainability. By defining macros utilizing the #outline directive, you possibly can create object-like macros for easy substitutions, function-like macros for advanced operations, and even chain-like macros for combining a number of macros.
Hope that is helpful, thanks.
You could wish to learn: Supply Code vs. Object Code, 10 Video games to Code in Scratch Programming, & Pig Sport in Python