Why use Macros in C? [duplicate]

Every few months I get an itch to go learn some bit of C that my crap college programming education never covered. Today it's macros. My basic understanding of macros is they're a simple search and replace that happens on your code prior to it being compiled. I'm having trouble understanding why you'd use macros. Most of the basic examples I'm looking at are something like

TEST(a,%d); #define TEST(a,b) printf(" The value of " #a " = " #b " \n", a) //which expands to printf(" The value of a = %d \n",a); 

(example from here) From my newbie point of view, it seems like defining a new function would give you the same results. I can see how historically macros would be useful for modifying a lot of source quickly in the days before easy search and replace, but something tells me I'm missing some bigger point. So what kind of useful things can macros do for you?

1 1 1 silver badge asked Aug 31, 2009 at 16:33 Alana Storm Alana Storm 166k 94 94 gold badges 414 414 silver badges 614 614 bronze badges

Using macros the way you suggest (modifying existing source code by redefining existing tokens instead of actually changing the code) is a really bad idea, unless you're just talking about changing the values of #defined constants.

Commented Aug 31, 2009 at 16:53 Dupe of stackoverflow.com/questions/653839/what-are-c-macros-useful-for among many, many others. Commented Aug 31, 2009 at 17:01

Certainly similar to those questions, but none that came up in search directly asked what made macros superior to just defining a function to do the work for you. I'm much happier with the answers here than in that other thread.

Commented Aug 31, 2009 at 17:23

All of the answerrs that appear here have appeared in similar questions regarding macros - there are, after all, a limited number of things you can do with macros.

Commented Aug 31, 2009 at 17:31

I disagree Neil. Quick example, this is the only thread that someone noted that macros can be used to avoid the creation of a new stack-frame, an answer that came about because the context of my question was different than the other questions.

Commented Aug 31, 2009 at 18:39

11 Answers 11

It's not exactly search and replace, it's token expansion. C macros are what every other kind of macro is in the computing world: a way to write something short and simple and have it automatically turn into something longer and more complicated.

One reason macros are used is performance. They are a way of eliminating function call overhead because they are always expanded in-line, unlike the "inline" keyword which is an often-ignored hint to the compiler, and didn't even exist (in the standard) prior to C99. For example, see the FD_ family of macros used in conjunction with the fd_sets used by select and pselect. These fd_sets are really just bitsets, and the FD_ macros are hiding bit twiddling operations. It would be annoying to write out the bit twiddling yourself every time, and a function call would be a lot of overhead for such a fast operation if it were not inlined.

Also, macros can do some things that functions cannot. Consider token pasting. Since the preprocessor runs before the compiler, it can make new identifiers for the compiler to use. This can give you a shorthand way to create lots of similar definitions, e.g.

#define DEF_PAIR_OF(dtype) \ typedef struct pair_of_##dtype < \ dtype first; \ dtype second; \ >pair_of_##dtype##_t DEF_PAIR_OF(int); DEF_PAIR_OF(double); DEF_PAIR_OF(MyStruct); /* etc */ 

Another thing it can do that a function could not is turn compile-time information into runtime information:

#ifdef DEBUG #define REPORT_PTR_VALUE(v) printf("Pointer %s points to %p\n", #v, v) #else #define REPORT_PTR_VALUE(v) #endif void someFunction(const int* reallyCoolPointer) < REPORT_PTR_VALUE(reallyCoolPointer); /* Other code */ >

There's no way that a function could use the name of its parameter in its output like the macro can. This also demonstrates compiling out debug code for release builds.