28

Possible Duplicate:
Inline functions vs Preprocessor macros

I want to know the difference between the inline function and macro function.

1) is inline function is the same of macro function ?

2) I know that both are not called but they are replaced by its code in the compilation phase. is not?

3) If there is difference, Could you specify it?

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 1
    http://msdn.microsoft.com/en-us/library/bf6bf4cf.aspx – CCoder Nov 14 '12 at 08:39
  • Thanks @CCoder. Here is the updated link: https://learn.microsoft.com/en-us/previous-versions/bw1hbe6y(v=vs.140)#inline-functions-vs-macros – Milan Jul 30 '21 at 16:16

4 Answers4

43

Inline replaces a call to a function with the body of the function, however, inline is just a request to the compiler that could be ignored (you could still pass some flags to the compiler to force inline or use always_inline attribute with gcc).

A macro on the other hand, is expanded by the preprocessor before compilation, so it's just like text substitution, also macros are not type checked, inline functions are. There's a comparison in the wiki.

For the sake of completeness, you could still have some kind of type safety with macros, using gcc's __typeof__ for example, the following generate almost identical code and both cause warnings if used with the wrong types:

#define max(a,b) \
  ({ __typeof__ (a) _a = (a); \
      __typeof__ (b) _b = (b); \
    _a > _b ? _a : _b; })

__attribute__((always_inline)) int max(int a, int b) {
   return (a > b ? a : b);
}

Note: sometimes typeless macros are just what's needed, for example, have a look at how uthash uses macros to make any C structure hashable without resorting to casts.

pytness
  • 357
  • 2
  • 13
iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • Could you please double-check the wiki link... I can't find the difference on that page. Thanks in advance! – Milan Jul 30 '21 at 16:12
33

1) No.

2) A Macro in C is simply text that is expanded before the compiler processes the source code. The inline keyword is used as a hint to the compiler that the function can be placed inline without the need for a call-stack to be set up.

So, for example, lets say that you got the following two snippets of code (First the macro and then the inline function):

#define MAX(x,y)     (x > y ? x : y)

and

inline int max(int x, int y) { return x > y ? x : y; }

When the preprocessor finds the following call in your code:

int highest = MAX (var_1, var_2);

it replaces it with

int highest = (var_1 > var_2 ? var_1 : var_2);

The above is what the compiler eventually gets during the compilation process, hence the snippet defined by MAX(x,y) is the replacement for MAX(var_1, var_2). When the compiler finds the function call

int highest = max (var_1, var_2);

Then the function "max" gets called. You must assume that it gets called the normal way, because the compiler is free to ignore your "inline" hint and make calls to the function go through the normal call-stack rather than simply placing the code for the function in the place it is encountered.

One last caveat with macros: because it is all text replacement and not code replacement, if you do something like this:

int highest = MAX (v1++, v2++);

the preprocessor will expand that to:

int highest = (v1++ > v2++ ? v1++ : v2++);

which is probably not what you intended.

Lelanthran
  • 1,510
  • 12
  • 18
  • 1
    Just to add something to the "last caveat with macros", macros should be always used with proper parenthesis! Every single macro argument should have parenthesis. So, `#define MAX(x,y) (x > y ? x : y)` should be defined as a `#define MAX(x,y) ((x) > (y) ?( x) :( y))`. With correct parenthesis the preprocessor expansion will be correct as well. – Ondrej Mar 31 '18 at 12:01
8

Macros are declarations that are substituted by the preprocessor (before the actual compile) The are not functions at all. Before the actual compile phase commences the macros are long-gone, and all that remains is their expansions (including expansion to nothing).

Inline functions are, in fact, language-compliant functions, with scope rules, variable declarations, logic constructs (loops, etc), and so on. An inline function is not expanded pre-compile-step like macros. They are compiled just as regular code is, but can be then injected (for lack of a better term) into compiled code and optimized as needed.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • you could still have scope {}, declarations and loops with macros – iabdalkader Nov 14 '12 at 09:41
  • @mux has someone been usurping the preprocessor language (which is all macros are) while I wasn't looking? I've not seen a control-loop defined in (and therefore substituted out after preprocess) C/C++ macros, but I'm certainly interested in seeing such an example. You can certainly have scope `{...}` in whatever the macro **emits** during the preprocess resolution phase, but if there is a way to do a `#for i=0` or `#while (true)` construct in preprocessor macros, i've never seen it (but again, would be interested in seeing your example). – WhozCraig Nov 14 '12 at 09:56
  • have a look at the source code of [uthash](http://www.codeforge.com/read/104731/uthash.h__html) a hash table (and linked list) library written completely with macros :) – iabdalkader Nov 14 '12 at 09:59
  • the macros in uthash all expand to `do {...} while(0)` for scope, anything can go there. – iabdalkader Nov 14 '12 at 10:01
  • @mux sure, but its the **emitted** substitution that introduces the C code (and thus the constructs) to compile therein. You said it yourself in the first sentence of the second paragraph of your answer: :"A macro on the other hand, is expanded by the preprocessor before compilation, so it's just like text substitution..." **That** is what I'm referring to. You can define all the #define's you want, but in the end its just substitution. What that substitution emits (**code**) is a completely different issue, I concur, but there is no #do #while in preprocessor, nor anything even close. – WhozCraig Nov 14 '12 at 10:11
  • I see what you mean now and no there isn't of course, but I didn't say that either, I said you could have those *with* macros not written in macros, miswording I guess, so that the end result would be the same. – iabdalkader Nov 14 '12 at 10:32
  • @mux oh for-sure. you can write Hamlet in macros. Some of the macro libs i've seen are astounding. Anyway for a moment I though I fell asleep on yet another technology. I slept clean through the C+11 release, and was thinking "I missed control constructs in the preprocessor too?? wtf?" S'all good (nice answer btw. I up-voted it, anyway). – WhozCraig Nov 14 '12 at 10:35
  • ha ha no this time you didn't miss anything, my bad, thanks anyway :) – iabdalkader Nov 14 '12 at 10:48
1

No inline is not same as macro definition, as macro is preproccessed but inline is just the indication to the compiler to put the whole body of the function where it is called.

putting inline keyword before any function definition is just the indication or request , compiler is free to choose whether it's going to make that function inline or leave it as normal

macro substitution : preprocessing phase (i.e. before compilation )

-- input file test.cresult file in linux test.i

inline substitution : compilation phase

Omkant
  • 9,018
  • 8
  • 39
  • 59