On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
Hi,
Could any one list possible number of diferences between a function
and a macro?
which one will be faster and why??
The macro will usually be faster, unless the compiler is smart
enough to inline the function call. Since C99 you can suggest the
compiler to do so by using the 'inline' keyword.
ex : function and a macro ti find max of two nums
#define MAX(a,b) (a>b) ? a:b
Please add enough parentheses:
((a) (b) ? a : (b))
As you wrote it, using complicated expressions could yield the
wrong semantic due to the precedence (actually, syntax) rules.
Note that the one returned will have been evaluated twice, so be
careful not to pass it expression with side effects.
and
int max(int a,int b)
This one doesn't suffer the problems described above, but it will
be much slower than the macro unless the compiler inlines it, and
it always convert the arguments to int and return an int, where
the macro return the "correct" type depending on the type of the
arguments.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen