difference between macro and function?

Answers were Sorted based on User's Feedback



difference between macro and function?..

Answer / subrat pattnaik

1.In Inline function if the codes are too much it acts as
normal function(without expanding in a line it gets jumps to
fun defination) but this is not with macro(it gets inserted
were it is calleed without checking its bulkness).
2.Suppose
#define square(x) (x*x)

inline void SQUARE(int x)
{
x*x;
}

void main()
{
a=5;
cout<<"The square is "<<square(++a);
cout<<"The SQUARE is "<<SQUARE(++a);
}
Here in macro it will b ++a * ++a(a incrementing 2 times)
In inline it will get incremented and is squared(++a)^2
Thanks

Is This Answer Correct ?    10 Yes 0 No

difference between macro and function?..

Answer / soruabh

When you call a function your compiler enters a
call-sequence (which takes
time) and allocates a new stack frame for that function
(whcih takes text
stack space) so that the function's body can be executed.
After it's done
you enter a returning-sequence phase (which takes time).



A macro does not need anything of the above, because it's
preprocessor's job
to expand a macro, it's only about text replacement, not
about compiler
stuff or code-generating issues. So you don't expend time
and space doing
what a function would need in order to be executed.

Is This Answer Correct ?    4 Yes 0 No

difference between macro and function?..

Answer / utpal kumar kashyap

Hi to Everyone,
See, inline function is a function whose code get inserted instead of jump to the function where it was called.
We can make any function as inline but there are few conditions in which compiler will not treat it as inline function....
If
1. Function contains any static variable.
2. It recursive.
3. Function code is large.

However, if function body is large and in this case if we try to make it as inline, then compiler wont give an error, but compiler would treat it as normal function. So idea is this, function code should be small for making it inline.

Is This Answer Correct ?    4 Yes 0 No

Post New Answer

More C++ General Interview Questions

Q1 On the screen how do you write the following words? she sells seashells by the seashore (a) all in one line (b) in three lines Q2 Write a program that asks interactively the user’s name and age and responds with Hello name, next year you will be next_age. where next_age is age + 1 Q3 For the different values of n, what is the output? printf(“%x %c %o %d”,n,n,n,n); (a) n = 67 (b) n = 20 (c) n = 128 (d) n = 255 (e) n = 100 Q4 What will be the output of the following program? int main() { char a,b,c; scanf(“%c %c %c”,&a,&b,&c); printf(“a=%c b=%c c=%c”,a,b,c); return 0; } [Note: The user input is:ABC DEF GHI]

1 Answers  


Evaluate !(1&&1||1&&0) a) Error b) False c) True

0 Answers  


What are member functions used in c++?

0 Answers  


Is c++ a programming language?

0 Answers  


Define copy constructor.

0 Answers  






What is c++ w3school?

0 Answers  


Is java a c++?

0 Answers  


What is static class data?

0 Answers  


Explain operator overloading.

0 Answers  


A mXn matrix is given and rows and column are sorted as shown below.Write a function that search a desired entered no in the matrix .with minimum complexity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

0 Answers  


What do you mean by global variables?

0 Answers  


What will strcmp("Astring", "Astring"); return a) A positive value b) A negative value c) Zero

0 Answers  


Categories