inline function vs function-like macro

Discussion in 'C Programming' started by subramanian100in@yahoo.com, India, Mar 6, 2007.

  1. , India

    , India Guest

    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 ?

    Given this, is there any reason for still preferring function-like
    macros over inline function feature of C99 ?
    , India, Mar 6, 2007
    #1
    1. Advertising

  2. , India

    santosh Guest

    , India wrote:
    > 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 ?
    >
    > Given this, is there any reason for still preferring function-like
    > macros over inline function feature of C99 ?


    Yes. Macros allow use without regard to type checking. Because they're
    a compile-time feature, they can be used to do certain things that
    would not be possible with functions. But more importantly, inline is
    a C99 feature and is not very portable across a wide range of
    compilers.

    Also inline specifies to the compiler to make access to the function
    as fast as possible, but doesn't actually require it to embed the code
    for the function, wherever it's called, unlike with macros.
    santosh, Mar 6, 2007
    #2
    1. Advertising

  3. , India

    Flash Gordon Guest

    , 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
    Flash Gordon, Mar 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nish
    Replies:
    4
    Views:
    484
    Thomas Stegen
    Oct 8, 2004
  2. TGOS
    Replies:
    3
    Views:
    375
    Kevin Bracey
    Feb 28, 2005
  3. Patrick Kowalzick
    Replies:
    5
    Views:
    469
    Patrick Kowalzick
    Mar 14, 2006
  4. Ajay
    Replies:
    5
    Views:
    384
    Pete Becker
    Apr 1, 2006
  5. Replies:
    7
    Views:
    441
Loading...

Share This Page