Macros in C with Examples

This C Tutorial explains Macros in C with examples.

Macros are like functions but aren’t true functions. By using macros, you can avoid repeating code and make your code more readable. To create a macro, you use the #define preprocessor directive. You can undefine a macro using the #undef directive.

Syntax of Macros:

#define MACRO_NAME value

The MACRO_NAME is the name of the macro and value is what will be substituted for MACRO_NAME when the macro is used. The value can be anything from a single word to a entire block of code. There must be no spaces between MACRO_NAME and value.

Types of Macros in C:

advertisement
advertisement
  • Object-like Macros
  • Function-like Macros
  • Variable-like Macros

Object-like Macros in C:

Object-like macros are defined using the #define preprocessor directive. They are expanded by the preprocessor whenever they are encountered in the code. Object-like macros do not take any arguments and can be used as if they were variables.

Function-like Macros in C:

Function-like macros are also defined using the #define preprocessor directive. However, they take arguments and are replaced by the preprocessor with the specified replacement text, which includes the arguments passed to the macro. Function-like macros can be used like functions.

Variable-like Macros in C:

Variable-like macros are a bit different. They are defined using the #define preprocessor directive, but they take arguments and are replaced by the preprocessor with the specified replacement text, which includes the arguments passed to the macro. Variable-like macros can be used like variables, but their values cannot be changed at runtime.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Macro Example:

#define MAX(a,b)   ((a) > (b) ? (a) : (b))

Let’s use it in a C program,

#include <stdio.h>
#define MAX(a,b)   ((a) > (b) ? (a) : (b))
 
int main(void)
{
    int x = 10, y = 15;
    float u = 2.0, v = 3.0;
    double s = 5, t = 5;
 
    printf("Max of two integers %d and %d is: %d\n", x, y, MAX(x,y));
    printf("Max of two floats %.2f and %.2f is: %.2f\n", u, v, MAX(u,v));
    printf("Max of two doubles %.2lf and %.2lfis: %lf\n", s, t, MAX(s,t));
 
    return 0;
}

Notice the output as follows,

advertisement
Max of two integers 10 and 15 is: 15
Max of two floats 2.00 and 3.00 is: 3.00
Max of two doubles 5.00 and 5.00is: 5.000000

Notice that same macro MAX(a,b) evaluated larger of two integers, two floats, two doubles etc.. This means macros are typeless. Had we used functions instead, we had have required three, one for each type of values. Besides, there’s overhead in functions’ calling and returning.

Then which of these two is more efficient than other?

Each has its own merits and demerits. Of course, macros are efficient than true functions when they are very short, one or two lines of code as for ex. MAX(a,b). Since preprocessor performs textual substitution in the program, therefore, it substitutes every occurrence of macro with its repacement text making program size huge unless macro is very small. Though, it doesn’t affect runtime efficiency of the program however it slows down compilation process. Every program has a single copy of each function which is called by calling program whenever required.

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.