473,763 Members | 7,622 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 #1
21 2323
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
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
MAx
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.

Sep 20 '07 #4
MAx wrote:
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.
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
MAx
On Sep 20, 1:51 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
MAx wrote:
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.

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
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
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
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
MAx wrote:
On Sep 20, 1:51 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
>MAx wrote:
>>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.
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

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

Similar topics

6
25558
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
10865
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
23
1921
by: yezi | last post by:
Hi, all: The 1st sendtence: int main(){ char string={""}; string = {" connected "}; ..... }
11
22372
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
5149
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
10148
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
10002
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
9938
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
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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.