Macro vs Function in C Macro vs Function in C c c

Macro vs Function in C


Macros are error-prone because they rely on textual substitution and do not perform type-checking. For example, this macro:

#define square(a) a * a

works fine when used with an integer:

square(5) --> 5 * 5 --> 25

but does very strange things when used with expressions:

square(1 + 2) --> 1 + 2 * 1 + 2 --> 1 + 2 + 2 --> 5square(x++) --> x++ * x++ --> increments x twice

Putting parentheses around arguments helps but doesn't completely eliminate these problems.

When macros contain multiple statements, you can get in trouble with control-flow constructs:

#define swap(x, y) t = x; x = y; y = t;if (x < y) swap(x, y); -->if (x < y) t = x; x = y; y = t; --> if (x < y) { t = x; } x = y; y = t;

The usual strategy for fixing this is to put the statements inside a "do { ... } while (0)" loop.

If you have two structures that happen to contain a field with the same name but different semantics, the same macro might work on both, with strange results:

struct shirt {    int numButtons;};struct webpage {    int numButtons;};#define num_button_holes(shirt)  ((shirt).numButtons * 4)struct webpage page;page.numButtons = 2;num_button_holes(page) -> 8

Finally, macros can be difficult to debug, producing weird syntax errors or runtime errors that you have to expand to understand (e.g. with gcc -E), because debuggers cannot step through macros, as in this example:

#define print(x, y)  printf(x y)  /* accidentally forgot comma */print("foo %s", "bar") /* prints "foo %sbar" */

Inline functions and constants help to avoid many of these problems with macros, but aren't always applicable. Where macros are deliberately used to specify polymorphic behavior, unintentional polymorphism may be difficult to avoid. C++ has a number of features such as templates to help create complex polymorphic constructs in a typesafe way without the use of macros; see Stroustrup's The C++ Programming Language for details.


Macro features:

  • Macro is Preprocessed
  • No Type Checking
  • Code Length Increases
  • Use of macro can lead to side effect
  • Speed of Execution is Faster
  • Before Compilation macro name is replaced by macro value
  • Useful where small code appears many time
  • Macro does not Check Compile Errors

Function features:

  • Function is Compiled
  • Type Checking is Done
  • Code Length remains Same
  • No side Effect
  • Speed of Execution is Slower
  • During function call, Transfer of Control takes place
  • Useful where large code appears many time
  • Function Checks Compile Errors


Side-effects are a big one. Here's a typical case:

#define min(a, b) (a < b ? a : b)min(x++, y)

gets expanded to:

(x++ < y ? x++ : y)

x gets incremented twice in the same statement. (and undefined behavior)


Writing multi-line macros are also a pain:

#define foo(a,b,c)  \    a += 10;        \    b += 10;        \    c += 10;        

They require a \ at the end of each line.


Macros can't "return" anything unless you make it a single expression:

int foo(int *a, int *b){    side_effect0();    side_effect1();    return a[0] + b[0];}

Can't do that in a macro unless you use GCC's statement expressions. (EDIT: You can use a comma operator though... overlooked that... But it might still be less readable.)


Order of Operations: (courtesy of @ouah)

#define min(a,b) (a < b ? a : b)min(x & 0xFF, 42)

gets expanded to:

(x & 0xFF < 42 ? x & 0xFF : 42)

But & has lower precedence than <. So 0xFF < 42 gets evaluated first.