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

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

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

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:
    753
    Thomas Stegen
    Oct 8, 2004
  2. TGOS
    Replies:
    3
    Views:
    561
    Kevin Bracey
    Feb 28, 2005
  3. Russell Shaw

    Function-like macro

    Russell Shaw, May 24, 2005, in forum: C Programming
    Replies:
    17
    Views:
    1,326
    Richard Bos
    May 27, 2005
  4. baumann@pan

    why function like macro doesn't work here?

    baumann@pan, Jul 8, 2005, in forum: C Programming
    Replies:
    8
    Views:
    575
    Sensei
    Jul 15, 2005
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    713
    Patrick Kowalzick
    Mar 14, 2006
  6. Ajay
    Replies:
    5
    Views:
    632
    Pete Becker
    Apr 1, 2006
  7. Replies:
    7
    Views:
    668
  8. jacob navia

    inline function or macro?

    jacob navia, Jan 2, 2010, in forum: C Programming
    Replies:
    16
    Views:
    801
    Flash Gordon
    Jan 3, 2010
Loading...