su**************@yahoo.com, India wrote, On 06/03/07 03:26:
Suppose I am using a compiler which is C99 compliant.
Function-like macros have the disadvantage if an argument with side-
effects is passed.
For example,
#define SQUARE(x) ( (x) * (x) )
If SQUARE(x++) is called, it results in undefined behaviour.
Instead we can use the C99 feature of inline function. Am I correct ?
Yes, given certain limitations.
Given this, is there any reason for still preferring function-like
macros over inline function feature of C99 ?
Yes, in certain situations. Taking your example, you can pass an int,
double or any other numeric type to SQUARE and it will "do the right
thing"(tm). However, if you use inline functions and you want to be able
to use it on all types without converting ints to doubles, or even a
complex type you will have to write squarei, squarel, squared, squarecd
etc. and call the correct one. Not always an issue, but a point to bare
in mind.
--
Flash Gordon