473,770 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between a MACRO and a FUNCTION

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
21 2324
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
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
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
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
On Sep 20, 2:04 am, MAx <mahesh1...@gma il.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 "replacemen t".
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
MAx <ma********@gma il.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_Keit h) 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
Richard Heathfield <rj*@see.sig.in validwrites:
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_Keit h) 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
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
On Thu, 20 Sep 2007 12:39:57 -0700, Keith Thompson wrote:
Richard Heathfield <rj*@see.sig.in validwrites:
>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
On Sep 20, 3:49 am, MAx <mahesh1...@gma il.comwrote:
On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@p obox.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
25561
by: Alexander Malkis | last post by:
Why do programmers like to use NDEBUG instead of DEBUG? -- Best regards, Alex. PS. To email me, remove "loeschedies" from the email address given.
2
5732
by: Aakash Jain | last post by:
Can someone explain me the difference between the following C++ macro and the function MAX: #define MAX(a, b) (((a) > (b)) ? (a) : (b)) template <class T> const T& MAX(const T& a, const T& b) { return (a > b ? a : b); }
7
1971
by: sachin_mzn | last post by:
Hi, It may be a silly question but I want to know the difference between #define macro and inline functions Is there any performance issue related to it. -Sachin
7
23555
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
8
10866
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
23
1923
by: yezi | last post by:
Hi, all: The 1st sendtence: int main(){ char string={""}; string = {" connected "}; ..... }
11
22380
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the type specified. This is much similar like a macro expansion in C. can anyone please explain advantages of one over the other ? Thanks in advance -
6
2312
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in the following small example ? #include <stdio.h>
11
5151
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.