Difference between a MACRO and a FUNCTION

Discussion in 'C Programming' started by MAx, Sep 20, 2007.

  1. MAx

    MAx Guest

    Hi,
    Could any one list possible number of diferences between a function
    and a macro?
    which one will be faster and why??

    ex : function and a macro ti find max of two nums
    #define MAX(a,b) (a>b) ? a:b

    and
    int max(int a,int b)
     
    MAx, Sep 20, 2007
    #1
    1. Advertising

  2. MAx

    Ian Collins Guest

    MAx wrote:
    > Hi,
    > Could any one list possible number of diferences between a function
    > and a macro?


    Homework?

    > which one will be faster and why??
    >

    The answer is either or neither.

    --
    Ian Collins.
     
    Ian Collins, Sep 20, 2007
    #2
    1. Advertising

  3. MAx

    Mark Bluemel Guest

    MAx wrote:
    > Hi,
    > Could any one list possible number of diferences between a function
    > and a macro?
    > which one will be faster and why??


    Do I smell homework?
     
    Mark Bluemel, Sep 20, 2007
    #3
  4. MAx

    MAx Guest

    On Sep 20, 1:10 pm, Mark Bluemel <> wrote:
    > MAx wrote:
    > > Hi,
    > > Could any one list possible number of diferences between a function
    > > and a macro?
    > > which one will be faster and why??

    >
    > Do I smell homework?


    obviously not a homework.
    this was an interview question...
    i am looking for an impressive answer.
     
    MAx, Sep 20, 2007
    #4
  5. MAx

    Mark Bluemel Guest

    MAx wrote:
    > On Sep 20, 1:10 pm, Mark Bluemel <> wrote:
    >> MAx wrote:
    >>> Hi,
    >>> Could any one list possible number of diferences between a function
    >>> and a macro?
    >>> which one will be faster and why??

    >> Do I smell homework?

    >
    > obviously not a homework.


    There's no "obviously" about it...

    > this was an interview question...
    > i am looking for an impressive answer.


    Then I suggest you do some research - other than asking in a newsgroup -
    and produce one.
     
    Mark Bluemel, Sep 20, 2007
    #5
  6. MAx

    MAx Guest

    On Sep 20, 1:51 pm, Mark Bluemel <> wrote:
    > MAx wrote:
    > > On Sep 20, 1:10 pm, Mark Bluemel <> wrote:
    > >> MAx wrote:
    > >>> Hi,
    > >>> Could any one list possible number of diferences between a function
    > >>> and a macro?
    > >>> which one will be faster and why??
    > >> Do I smell homework?

    >
    > > obviously not a homework.

    >
    > There's no "obviously" about it...
    >
    > > this was an interview question...
    > > i am looking for an impressive answer.

    >
    > Then I suggest you do some research - other than asking in a newsgroup -
    > and produce one.


    Thanks for the help
     
    MAx, Sep 20, 2007
    #6
  7. MAx

    Army1987 Guest

    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
     
    Army1987, Sep 20, 2007
    #7
  8. Army1987 said:
    > On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:


    <snip>

    >> #define MAX(a,b) (a>b) ? a:b

    >
    > Please add enough parentheses:
    > ((a) > (b) ? a : (b))


    You missed some.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 20, 2007
    #8
  9. Richard Heathfield wrote:
    > Army1987 said:
    >> On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:

    >
    > <snip>
    >
    >>> #define MAX(a,b) (a>b) ? a:b

    >> Please add enough parentheses:
    >> ((a) > (b) ? a : (b))

    >
    > You missed some.
    >
    > <snip>


    Really?

    (((a)>(b)) ? a : (b))

    has the same meaning, as does

    ((a)>(b) ? (a) : (b))

    Though this does look nicer due to consistency.

    The only other places I can think to add parentheses are either redundant ((a))
    or obviously wrong ((a) > ((b) ? a : (b)))

    So what did he miss?

    Phil

    --
    Philip Potter pgp <at> doc.ic.ac.uk
     
    Philip Potter, Sep 20, 2007
    #9
  10. MAx

    Mark Bluemel Guest

    MAx wrote:
    > On Sep 20, 1:51 pm, Mark Bluemel <> wrote:
    >> MAx wrote:
    >>> On Sep 20, 1:10 pm, Mark Bluemel <> wrote:
    >>>> MAx wrote:
    >>>>> Hi,
    >>>>> Could any one list possible number of diferences between a function
    >>>>> and a macro?
    >>>>> which one will be faster and why??
    >>>> Do I smell homework?
    >>> obviously not a homework.

    >> There's no "obviously" about it...
    >>
    >>> this was an interview question...
    >>> i am looking for an impressive answer.

    >> Then I suggest you do some research - other than asking in a newsgroup -
    >> and produce one.

    >
    > Thanks for the help
    >

    You're welcome. I wasn't just being snitty (though that's probably part
    of it).

    This actually does, in effect, come under the "homework" heading. The
    point of the exercise should be for you to learn something for yourself
    by putting in the necessary effort, rather than being spoonfed something
    which you can hand in to your tutor or spout to your interviewer.
     
    Mark Bluemel, Sep 20, 2007
    #10
  11. MAx

    Army1987 Guest

    On Thu, 20 Sep 2007 11:04:55 +0000, Richard Heathfield wrote:

    > Army1987 said:
    >> On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:

    >
    > <snip>
    >
    >>> #define MAX(a,b) (a>b) ? a:b

    >>
    >> Please add enough parentheses:
    >> ((a) > (b) ? a : (b))

    >
    > You missed some.

    Syntax
    1 conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

    (As someone pointed out on this newsgroup, ? and : can only occur
    in pairs, so they are like [ and ], or ( and ) for this matter.)
    --
    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
     
    Army1987, Sep 20, 2007
    #11
  12. Philip Potter said:

    > Richard Heathfield wrote:
    >> Army1987 said:
    >>> On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:

    >>
    >> <snip>
    >>
    >>>> #define MAX(a,b) (a>b) ? a:b
    >>> Please add enough parentheses:
    >>> ((a) > (b) ? a : (b))

    >>
    >> You missed some.
    >>
    >> <snip>

    >
    > Really?


    No, not really - at least, I just tried three times to break his version
    and failed each time. So I owe him an apology.

    > (((a)>(b)) ? a : (b))
    >
    > has the same meaning, as does
    >
    > ((a)>(b) ? (a) : (b))
    >
    > Though this does look nicer due to consistency.


    Yes, it does, doesn't it? Emerson notwithstanding...

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 20, 2007
    #12
  13. MAx

    Army1987 Guest

    On Thu, 20 Sep 2007 11:44:58 +0000, Richard Heathfield wrote:
    > Philip Potter said:
    >> Richard Heathfield wrote:
    >>> Army1987 said:
    >>>> Please add enough parentheses:
    >>>> ((a) > (b) ? a : (b))
    >>> You missed some.

    [snip]
    >> (((a)>(b)) ? a : (b))
    >>
    >> has the same meaning, as does
    >>
    >> ((a)>(b) ? (a) : (b))
    >>
    >> Though this does look nicer due to consistency.

    >
    > Yes, it does, doesn't it? Emerson notwithstanding...


    It does (maybe whitespace around > will add even more
    consistency?), but who's Emerson?
    --
    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
     
    Army1987, Sep 20, 2007
    #13
  14. Army1987 wrote:

    > It does (maybe whitespace around > will add even more
    > consistency?), but who's Emerson?


    "A foolish consistency is the hobgoblin of little minds."
    -- Ralph Waldo Emerson (1803–1882)

    Don't think I'm a literary person, but I remembered the line from
    "What's Up, Doc". :)
     
    Eberhard Schefold, Sep 20, 2007
    #14
  15. MAx

    John Bode Guest

    On Sep 20, 2:04 am, MAx <> wrote:
    > Hi,
    > Could any one list possible number of diferences between a function
    > and a macro?


    Grab your handy C reference and read the section about the
    preprocessor, focusing on the terms "definition" and "replacement".
    After that, it should be quite obvious what the differences between a
    function and a function-like macro are.

    > which one will be faster and why??


    That depends on the operations involved. For your example below, all
    things being equal, the macro should be faster. Again, once you've
    read up on what exactly the preprocessor does with a function-like
    macro definition, the reason why should be fairly evident.

    >
    > ex : function and a macro ti find max of two nums
    > #define MAX(a,b) (a>b) ? a:b
    >
    > and
    > int max(int a,int b)
     
    John Bode, Sep 20, 2007
    #15
  16. MAx <> writes:
    > Could any one list possible number of diferences between a function
    > and a macro?
    > which one will be faster and why??
    >
    > ex : function and a macro ti find max of two nums
    > #define MAX(a,b) (a>b) ? a:b
    >
    > and
    > int max(int a,int b)


    Is this homework?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 20, 2007
    #16
  17. Richard Heathfield <> writes:
    > Philip Potter said:
    >> Richard Heathfield wrote:
    >>> Army1987 said:
    >>>> On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
    >>>
    >>> <snip>
    >>>
    >>>>> #define MAX(a,b) (a>b) ? a:b
    >>>> Please add enough parentheses:
    >>>> ((a) > (b) ? a : (b))
    >>>
    >>> You missed some.
    >>>
    >>> <snip>

    >>
    >> Really?

    >
    > No, not really - at least, I just tried three times to break his version
    > and failed each time. So I owe him an apology.

    [...]

    Nevertheless, I would certainly use parentheses myself:
    #define MAX(a, b) ((a) > (b) ? (a) : (b))
    rather than
    #define MAX(a, b) ((a) > (b) ? a : (b))

    Consistency is certainly part of it. Another part is that I'm *still*
    not 100% convinced that the parentheses are unnecessary. I have a
    nagging hunch that there *might* be an argument that would break the
    first form, perhaps something involving the ?: operator in the operand
    itself. Or perhaps there's some argument that should be a syntax
    error, but that will cause the unparenthesized version to expand to a
    legal expression that doesn't do what you want.

    It's very likely that my concern is groundless, and that the
    unparenthesized form is perfectly safe. But if it takes an
    experienced C programmer like RH three tries, not to prove that it's
    safe, but merely to fail to prove that it isn't, then it's likely to
    waste some of the time of every attentive programmer who reads it. If
    I'm tracking down a bug that manifests itself anywhere near any use of
    this macro, I'm likely to waste time convincing myself that the macro
    isn't the problem before persuing other possibilities.

    We had something similar here recently, with a macro definition
    something like:

    #define N -42

    Several people immediately complained that it *should* be

    #define N (-42)

    but it took a day or two before anyone was able to present an example
    where it actually made a difference.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 20, 2007
    #17
  18. Keith Thompson said:

    > I have a
    > nagging hunch that there *might* be an argument that would break the
    > first form, perhaps something involving the ?: operator in the operand
    > itself.


    That was my principal method of attack. I couldn't break it that way, but
    that doesn't mean it can't be broken. (Nor does it mean it can,
    obviously.)

    I suspect that a grammatical analysis will bear more fruit than any number
    of attempts at a crack.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 20, 2007
    #18
  19. On Thu, 20 Sep 2007 12:39:57 -0700, Keith Thompson wrote:
    > Richard Heathfield <> writes:
    >> Philip Potter said:
    >>> Richard Heathfield wrote:
    >>>> Army1987 said:
    >>>>> On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
    >>>>
    >>>> <snip>
    >>>>
    >>>>>> #define MAX(a,b) (a>b) ? a:b
    >>>>> Please add enough parentheses:
    >>>>> ((a) > (b) ? a : (b))
    >>>>
    >>>> You missed some.
    >>>>
    >>>> <snip>
    >>>
    >>> Really?

    >>
    >> No, not really - at least, I just tried three times to break his
    >> version and failed each time. So I owe him an apology.

    > [...]
    >
    > Nevertheless, I would certainly use parentheses myself:
    > #define MAX(a, b) ((a) > (b) ? (a) : (b))
    > rather than
    > #define MAX(a, b) ((a) > (b) ? a : (b))
    >
    > Consistency is certainly part of it. Another part is that I'm *still*
    > not 100% convinced that the parentheses are unnecessary. I have a
    > nagging hunch that there *might* be an argument that would break the
    > first form, perhaps something involving the ?: operator in the operand
    > itself.


    The relevant rules from the grammar are:

    primary-expression:
    ( expression )

    conditional-expression:
    logical-OR-expression ? expression : conditional-expression

    Assuming a is an expression, it is valid in both forms. If it's not
    valid, it's not an expression. Even if a contains a nested conditional
    expression, for example in MAX(1 ? 2 : 3, 4 ? 5 : 6), the compiler will
    do the right thing, since you're allowed to write an expression

    ((1 ? 2 : 3) > (4 ? 5 : 6) ? 1 ? 2 : 3 : (4 ? 5 : 6))

    with no more a problem with the ?: nesting than with the () nesting.

    > Or perhaps there's some argument that should be a syntax error,
    > but that will cause the unparenthesized version to expand to a legal
    > expression that doesn't do what you want.


    #define A 0)==(0

    The parenthesised version will accept MAX(A, A), as it expands to

    ((0)==(0) > (0)==(0) ? (0)==(0) : (0)==(0))

    while the unparenthesised version will show that this is a syntax error.
    I don't consider this a good reason to not parenthesise a. Similarly, if
    a similar construct is found that's valid with the second definition, but
    not with the first, I don't consider it a good reason to include the
    parentheses. (For what it's worth, I tried finding one, but failed to.)

    Personally, I'd probably parenthesise a too, so that preprocessor output
    is a little bit easier to read, and because I don't see a downside to it,
    but it's not necessary.
     
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F, Sep 20, 2007
    #19
  20. MAx

    John Bode Guest

    On Sep 20, 3:49 am, MAx <> wrote:
    > On Sep 20, 1:10 pm, Mark Bluemel <> wrote:
    >
    > > MAx wrote:
    > > > Hi,
    > > > Could any one list possible number of diferences between a function
    > > > and a macro?
    > > > which one will be faster and why??

    >
    > > Do I smell homework?

    >
    > obviously not a homework.
    > this was an interview question...
    > i am looking for an impressive answer.


    If you don't already have a good answer for that particular question
    (or know where to look it up), then you shouldn't be interviewing for
    a job that involves C programming.
     
    John Bode, Sep 20, 2007
    #20
    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. jakk
    Replies:
    4
    Views:
    12,506
  2. Aakash Jain
    Replies:
    2
    Views:
    13,477
    David Harmon
    Apr 13, 2004
  3. Newbie_sw2003

    What is the difference between MACRO and FUNCTION?

    Newbie_sw2003, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    20,077
    Ed Morton
    Sep 11, 2003
  4. lasek

    Difference between Macro and Function...

    lasek, Oct 21, 2004, in forum: C Programming
    Replies:
    8
    Views:
    997
    Robert Wessel
    Oct 22, 2004
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    496
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page