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?
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.