Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

for a lot of quick tasks where one could employ a function f(x,y), in plain C, macros are used. I would like to ask specifically about these cases, that are solvable by a function call (i.e. macros used for inlining functions, not for code expansion of arbitrary code).

Typically C functions are not inlined since they might be linked to from other C files. However, static C functions are only visible from within the C file they are defined in. Therefore they can be inlined by compilers. I have heard that a lot of macros should be replaced by turning them into static functions, because this produces safer code.

Are there cases where this is a not good idea?

Again: Not asking about Code-Production macros with ## alike constructs that cannot at all be expressed as a function.

share|improve this question
I think your question might have been answered here and here – Maru Oct 18 '13 at 9:09
writing a proper macro is a pain, and inlineing functions just ensures it can be defined multiple times in the binary, the compiler can decide whether it will actually inline it to maybe save space – ratchet freak Oct 18 '13 at 9:20
I would actually suspect most of these are historical reasons. Ages ago, the C compilers didn't inline functions at all. So people used macros for things they wanted inlined. C code is usually unusually long-lived. – Jan Hudec Oct 18 '13 at 10:59
@ratchetfreak: C does not have inline, this is not about C++. – data_hope Oct 18 '13 at 11:25
@wirrbel Of course there is inline in C, at least since C99. For a quick overview, see Wikipedia on inline functions‌​. – amon Oct 18 '13 at 14:12
up vote 5 down vote accepted

Macros that are solvable by a function call have a lot of pitfalls:

  • They are hard to write, because you may have to handle properly arguments like ++i.
  • They are hard to debug with a visual debugger since you cannot step through a macro or put a breakpoint there.
  • They are hard to handle correctly when analysing compilations dependencies.

Macros that are solvable by a function call could have been useful to provide inlining in a primitive compiler. I am not aware of any compiler not handling inline functions and some can even inline across compilation units.

Typically C functions are not inlined since they might be linked to from other C files.

There is no reason why a compiler would not be able to provide two versions of a function, a traditional called one and an inlined one. You should look at the documentation of the compiler you are targetting. Also, you may want to look at generated assembly: even if you do not know assembly, you can quickly learn to tell if some function has been inlined or not. (Start with baby-examples to quickly learn this.)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.