Macros in programming C – Unicminds

by Brenden Burgess

When you buy through links on our site, we may earn a commission at no extra cost to you. However, this does not influence our evaluations.

The #define directive for macros

In C, we define the macros using the pre-inquesting “#define” directive. The pre -processor guidelines in C always begin with a “#” symbol. The pre -processor guidelines are used to fulfill the expansion of macro, file inclusions, conditional compilation and other directives.

Macros name

As a convention, macro names are written in capital letters. Programs are easier to read when it is possible to say at a glance what names are macros.

Example:

#include

#define Pi 3.1428

int hand ()

{

Floating circumference = 0, radius = 0;

Printf (“Enter your name:“);

Printf (“Enter your name:“);

scanf (“% f” and radius);

circumference = 2 * pi * radius;

printf (“circumference of the circle =% f \ n”, circumference);

Back 0;

}

Macros in programming CMacros in programming C

During the pre -treatment, the front compilation phase, all macros are replaced by real values. Remember that this happens before the compilation is finished. Thus, the program size increases after pre -treatment.

Macros types

And, macros do not need to be a value, they can also do a simple function like working with macros that can have arguments like functions. Whenever the name is used, it is replaced by the content of the macro.

#define Circlearaea (R) ((3.14) * R * r)

But why use a macro in the first place? And why not just use a constant variable or variable. The macros offer a slight advantage of speed compared to the use of functions because no time is used to pass arguments and control, but even this advantage is countered by intelligently optimized compilers these days.

Macros vs constant

What is preference – Macros or constant variables?

A variable is the safe type and uses reach rules. As far as possible, you must try to be within the limits of type security. A constant variable needs a storage location while a macro does not need storage. However, modern compilers also replace the constants so that they are optimized in space. A constant variable requires initialization while a macro needs no initialization.

Thus, unless you work on integrated systems or real -time systems where each byte or each Nano Second Account (and you also fear that the compiler is unable to optimize your constant variable), the use of constant variables is recommended on macros for safety and maintainability. In the end, it is a personal choice of coding style, but the coding style that is based on macros seems to be more buggy.

Conditional compilation using macros

Conditional compilation in C is based on several directives of pre -processors:

  • #ifdef
  • #ifndef
  • #if
  • #other
  • #Elif
  • #endif

These directives control if the compiler includes or excludes parts of the code according to the assessment of the conditions at the time of the compilation. A popular application of this is when you want to compile different types of code for different platforms earns 32, win64, Linux, etc.

Conclusion

Macros in C programming provide a powerful mechanism to replace code extracts and values ​​throughout your program. They improve the readability of the code, reduce redundancy and improve maintaability. By defining macros using the #define directive, you can create object type macros for simple substitutions, function macros for complex operations and even chain macros to combine several macros.

I hope it's useful, thank you.

You may like to read: Source code vs object code,, 10 games to cod in scratch programming& Python pork game

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.