By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,857 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

Difference between a MACRO and a FUNCTION

P: n/a
MAx
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)

Sep 20 '07 #1
Share this Question
Share on Google+
21 Replies


P: n/a
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.
Sep 20 '07 #2

P: n/a
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?
Sep 20 '07 #3

P: n/a
MAx
On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
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.

Sep 20 '07 #4

P: n/a
MAx wrote:
On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
>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.
Sep 20 '07 #5

P: n/a
MAx
On Sep 20, 1:51 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
MAx wrote:
On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
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

Sep 20 '07 #6

P: n/a
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

Sep 20 '07 #7

P: n/a
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
Sep 20 '07 #8

P: n/a
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 <atdoc.ic.ac.uk
Sep 20 '07 #9

P: n/a
MAx wrote:
On Sep 20, 1:51 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
>MAx wrote:
>>On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
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.
Sep 20 '07 #10

P: n/a
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

Sep 20 '07 #11

P: n/a
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
Sep 20 '07 #12

P: n/a
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

Sep 20 '07 #13

P: n/a
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 (18031882)

Don't think I'm a literary person, but I remembered the line from
"What's Up, Doc". :-)
Sep 20 '07 #14

P: n/a
On Sep 20, 2:04 am, MAx <mahesh1...@gmail.comwrote:
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)


Sep 20 '07 #15

P: n/a
MAx <ma********@gmail.comwrites:
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) ks***@mib.org <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"
Sep 20 '07 #16

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
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) ks***@mib.org <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"
Sep 20 '07 #17

P: n/a
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
Sep 20 '07 #18

P: n/a
On Thu, 20 Sep 2007 12:39:57 -0700, Keith Thompson wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
>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.
Sep 20 '07 #19

P: n/a
On Sep 20, 3:49 am, MAx <mahesh1...@gmail.comwrote:
On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
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.

Sep 20 '07 #20

P: n/a
In article <fc**********@news1.zwoll1.ov.home.nl>,
$)CHarald van D )& k <tr*****@gmail.comwrote:
>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.
Yes. For it to break when the parenthesized version works, there
would have to be something that is parsable as an expression when
parenthesized, but not otherwise. All parentheses do is turn an
expression into a primary-expression, so if parentheses make it
an expression it already was one.

[We can see that parentheses are needed for the third operand, since
there *are* things that are parsable as a conditional-expression when
parenthesized, but not otherwise: a=b for example.]

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #21

P: n/a
On Thu, 20 Sep 2007 19:46:47 +0000, Richard Heathfield wrote:
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.
Given the syntax and the fact that the only place where the token
? and the token : can occur is a conditional expression, adding
( after ?, or ) before :, should be no more useful that adding
( after [, or ) before ].
Compare:

Syntax
1 postfix-expression:
primary-expression
postfix-expression [ expression ]
<snip>

and:

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

(Yes, there could be some mysterious, hidden point that I'm
missing, considering that I was almost as self-assured as now when
I claimed that #define INT_MIN -128 couldn't possibly be broken.)
--
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

Sep 20 '07 #22

This discussion thread is closed

Replies have been disabled for this discussion.