UnicMinds

Macros in C Programming - UnicMinds

Macros in C Programming

The #define Directive for Macros

In C, we define macros using “#define” preprocessor directive. Preprocessor directives in C always start with a “#” symbol. Preprocessor directives are used to accomplish macro expansion, file inclusions, conditional compilation, and other directives.

Naming of Macros

As a convention, macro names are written in upper case. Programs are easier to read when it is possible to tell at a glance which names are macros.

Example:

#include <stdio.h>

#define PI 3.1428

int main()  

{

float circumference=0, radius=0;

printf(“Enter your name: “);

printf(“Enter your name: “);

scanf(“%f”, &radius);

circumference = 2*PI*radius;

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

return 0;

}

Macros in C Programming

While preprocessing, the phase before compilation, all the macros get replaced with the actual values. Remember this happens before the compilation is done. So, the size of the program increases after preprocessing.

Types of Macros

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

#define CIRCLEAREA(r)  ((3.14)*r*r)

But, why use a macro in the first place? And, why not just use a variable or a constant variable. Macros offer a slight benefit in speed compared to using functions because no time is used in passing arguments and control, but even that benefit is countered by intelligently optimized compilers these days.

Macros vs. Constants

Which is preferable – macros or constant variables? 

A variable is type safe and uses scoping rules. As much as possible, one should try to be within the boundaries of type safety. A constant variable needs a storage location whereas a macro doesn’t need storage. But, the modern compilers too replace constants so that it is space optimized. A constant variable needs initialization whereas a macro doesn’t need any initialization.

So, unless you are working on embedded systems or real time systems where every byte or every nano second counts (and you are also worried that the compiler will not be able to optimize away your constant variable), use of constant variables is recommended over macros for the sake of safety and maintainability. In the end it is a personal choice of coding style, but the coding style that over relies on macros seems to be more buggy.

Conditional Compilation using Macros

Conditional compilation in C relies on several preprocessor directives:

  • #ifdef
  • #ifndef
  • #if
  • #else
  • #elif
  • #endif

These directives control whether the compiler includes or excludes parts of the code based on evaluating conditions at compile time. A popular application of this is when you want to compile different types of code for different platforms Win 32, Win64, Linux, etc.

Conclusion

Macros in C programming provide a powerful mechanism for replacing code snippets and values throughout your program. They enhance code readability, reduce redundancy, and improve maintainability. By defining macros using the #define directive, you can create object-like macros for simple substitutions, function-like macros for complex operations, and even chain-like macros for combining multiple macros.

Hope this is useful, thank you.

You may like to read: Source Code vs. Object Code, 10 Games to Code in Scratch Programming, & Pig Game in Python

Leave a Comment

Your email address will not be published. Required fields are marked *

BOOK A FREE TRIAL