Tuesday, May 25, 2010

INLINE FUNCTIONS VS MACROS

INLINE FUNCTION VS MACROS: the old battle :)

I'm sure sometime or the other you might have had this question popping out of your mind-
"I’m confused about the use of inline functions and macros.What is the difference between them?"


Well the answer is simple->


Inline functions are similar to macros because they both are expanded at compile time, but the macros are expanded by the preprocessor, while inline functions are parsed by the compiler. There are several important differences:
  • Inline functions follow all the protocols of type safety enforced on normal functions.
  • Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration.
  • Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.


Now the BIG QUESTION-
When should I use macro and when inline functions?


Besides the difference already pointed out, you also must have in mind that because macros are expanded at pre-compile time, you cannot use them for debugging, but you can use inline functions. This is one important judging factor, isn't it?

No comments:

Post a Comment