GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 69 of 203    Print  
What is the difference between macro and inline()?

  
Total Answers and Comments: 9 Last Update: February 24, 2009     Asked by: karishma 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: poojagupta84
 

XIV

Inline Function

Preprocessor Macro – good for declaring constnts eg. #define PI 3.14
Provide textual substitution
Each time the macro name is encountered with arguments, the arguments used in its definition are replaced by the actual arguments found.

1

It is a function provided by C++.
It is declared by using the keyword inline before the function protoype.

It is a a preprocessor directive provided by C.
It is declared by using the preprocessor directive #define before the actual statement.

2

Expressions passed as arguments to inline functions are evaluated once.

In some cases, expressions passed as arguments to macros can be evaluated more than once.
Every time you use an argument in a macro, that argument is evaluated.

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

void main()

{

  int a = 0;

  int b = 1;

  int c = max(a++, b++);

  cout << a << endl << b << endl;

}

The intention is to have the program print 1 and 2, but because the macro is expanded to:

int c = a++ > b++ ? a++ : b++;

b will be incremented twice, and the program prints 1 and 3. //try

3

They are parsed by the compiler

They are expanded by the preprocessor at pre-compile time.

4

Inline functions follow all the protocols of type safety enforced on normal functions.
Argument types are checked, and necessary conversions are performed correctly.
The compiler performs return type checking, function signature before putting inline function into symbol table.
They can be overloaded to perform the right kind of operation for the right kind of data.

No such thing, hence, macros are more error prone as compared to inline functions. the The parameters are not typed (the macro works for any objects of arithmetic type).
No error checking is done during compilation.
Eg. you can pass strings to a macro that does some integer arithmetic

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

 

int main( void)

{

  cout << "Maximum of 10 and 20 is " << MAX("20", "10") << endl;

    return 0;

}

5

Can be used for debugging a program as they are expanded at compile time and a break point caa be placed at the inline function adefinition and step into the method for debugging step by step.

Can not be used for debugging as they are expanded at pre-compile time.

6

Inline member functioncs can access the class’s member data.

The preprocessor has no permission to access member data of a class and are thus useless even if used within a class. Thus they can not even be used as member functions.
Eg.

#define VAL (X::i) // Error

class X {

  int i;

public:
  ---};

7

 

Paranthesis need to be handled carefully as missing a pair or putting an extra pair may prouce undesirable results.

8

Inline functions may or may not be expanded by the compiler. It is the compiler’s decision whether to expand the function inline or not.

Macros are always expanded.

9

It can be defined inside or outside the class.

It can not be defined insode the class.

10

Inline functions pass arguments by value, just like regular functions do. If the argument is an expression such as 4.5 +7.5 then the function passes the value of the expression 12 in this case.

Expressions passed into macros are not always evaluated before entering the macro body.

Macros don’t pass by value.

Thus , #define square(x) x*x

:

b=square(4.5+7.5)

This will be replaced by : b=4.5+7.5*4.5+7.5

11

 Since type checking can be used in inline functions, theis problem can be avoided.

Expressions may expand inside the macro so that their evaluation precedence is different from what you expect.

Eg.

#define FLOOR(x,b) x>=b?0:1

Now, if expressions are used for the arguments

if(FLOOR(a&0x0f,0x07)) // ...

the macro will expand to

if(a&0x0f>=0x07?0:1)

The precedence of & is lower than that of >=, so the macro evaluation will surprise you.



Above answer was rated as good by the following members:
bhavnajoshi, emrold.j
March 01, 2006 07:01:54   #1  
Madhu        

RE: What is the difference between macro and inline()?...
Inline functions are similar to macros because they both are expanded at compile time but the macros are expanded by the preprocessor while inline functions are parsed by the compiler. There are several important differences: Inline functions follow all the protocols of type safety enforced on normal functions. Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration. Expressions passed as arguments to inline functions are evaluated once. In some cases expressions passed as arguments to macros can be evaluated more than once.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 09, 2006 05:56:16   #2  
Jai        

RE: What is the difference between macro and inline()?...

1. Inline follows strict parameter type checking macros do not.

2. Macros are always expanded by preprocessor whereas compiler may or may not replace the inline definitions.


 
Is this answer useful? Yes | No
January 31, 2007 12:45:33   #3  
Nitin Bhatt        

RE: What is the difference between macro and inline()?...
Hi I was asked the same question in one of the interviews I faced recently. I gave the answer:



1. Inline follows strict parameter type checking macros do not.



2. Macros are always expanded by preprocessor whereas compiler may or may not replace the inline definitions.



The next question was based on point 2



The question was can I force the compiler to replace the inline definition.Please let me know if anyone has the answer to this question.



Thanks in advance.

Nitin

 
Is this answer useful? Yes | No
March 09, 2007 05:27:43   #4  
Dinesh Jethva        

RE: What is the difference between macro and inline()?...
No You cant force compiler to make a function inline. It is purely compiler based decision.
 
Is this answer useful? Yes | No
May 29, 2007 13:27:52   #5  
NALINI        

RE: What is the difference between macro and inline()?...
  • In INLINE functions the control replaces the code where it is called that function AND it follows strict type checking..
  • In MACROS control is transferred to place where macro is present AND it doesn't follow strict type checking..


 
Is this answer useful? Yes | No
September 06, 2007 09:06:54   #6  
shruti        

RE: What is the difference between macro and inline()?...
Nitin I think we can force compiler to take function as inline bcoz its the size of the function mainly that has to be small if a function is inline and a large function can be made inline by decreasing its size

 
Is this answer useful? Yes | No
November 16, 2007 13:17:53   #7  
Shruthi D. R        

RE: What is the difference between macro and inline()?...
inline will be expanded by compiler depending on the code size.
Code size will be decided by compiler.
If it expands it acts like macro else as a function

 
Is this answer useful? Yes | No
May 08, 2008 18:00:23   #8  
poojagupta84 Member Since: April 2008   Contribution: 1    

RE:

XIV

Inline Function

Preprocessor Macro – good for declaring constnts eg. #define PI 3.14
Provide textual substitution
Each time the macro name is encountered with arguments the arguments used in its definition are replaced by the actual arguments found.

1

It is a function provided by C++.
It is declared by using the keyword inline before the function protoype.

It is a a preprocessor directive provided by C.
It is declared by using the preprocessor directive #define before the actual statement.

2

Expressions passed as arguments to inline functions are evaluated once.

In some cases expressions passed as arguments to macros can be evaluated more than once.
Every time you use an argument in a macro that argument is evaluated.

#define max(a b) (a>b?a:b)

void main()

{

int a 0;

int b 1;

int c max(a++ b++);

cout << a << endl << b << endl;

}

The intention is to have the program print 1 and 2 but because the macro is expanded to:

int c a++ > b++ ? a++ : b++;

b will be incremented twice and the program prints 1 and 3. //try

3

They are parsed by the compiler

They are expanded by the preprocessor at pre-compile time.

4

Inline functions follow all the protocols of type safety enforced on normal functions.
Argument types are checked and necessary conversions are performed correctly.
The compiler performs return type checking function signature before putting inline function into symbol table.
They can be overloaded to perform the right kind of operation for the right kind of data.

No such thing hence macros are more error prone as compared to inline functions. the The parameters are not typed (the macro works for any objects of arithmetic type).
No error checking is done during compilation.
Eg. you can pass strings to a macro that does some integer arithmetic

#define MAX(a b) ((a < b) ? b : a)

int main( void)

{

cout << "Maximum of 10 and 20 is " << MAX("20" "10") << endl;

return 0;

}

5

Can be used for debugging a program as they are expanded at compile time and a break point caa be placed at the inline function adefinition and step into the method for debugging step by step.

Can not be used for debugging as they are expanded at pre-compile time.

6

Inline member functioncs can access the class’s member data.

The preprocessor has no permission to access member data of a class and are thus useless even if used within a class. Thus they can not even be used as member functions.
Eg.

#define VAL (X::i) // Error

class X {

int i;

public:
---};

7

Paranthesis need to be handled carefully as missing a pair or putting an extra pair may prouce undesirable results.

8

Inline functions may or may not be expanded by the compiler. It is the compiler’s decision whether to expand the function inline or not.

Macros are always expanded.

9

It can be defined inside or outside the class.

It can not be defined insode the class.

10

Inline functions pass arguments by value just like regular functions do. If the argument is an expression such as 4.5 +7.5 then the function passes the value of the expression 12 in this case.

Expressions passed into macros are not always evaluated before entering the macro body.

Macros don’t pass by value.

Thus #define square(x) x*x

:

b square(4.5+7.5)

This will be replaced by : b 4.5+7.5*4.5+7.5

11

Since type checking can be used in inline functions theis problem can be avoided.

Expressions may expand inside the macro so that their evaluation precedence is different from what you expect.

Eg.

#define FLOOR(x b) x> b?0:1

Now if expressions are used for the arguments

if(FLOOR(a&0x0f 0x07)) // ...

the macro will expand to

if(a&0x0f> 0x07?0:1)

The precedence of & is lower than that of > so the macro evaluation will surprise you.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 24, 2009 03:58:01   #9  
bhavnajoshi Member Since: February 2009   Contribution: 3    

RE: What is the difference between macro and inline()?

1. In macro the format is used #define

but inline function use inline keyword additional in function prototype format.


2. When we use macro definition

like_#define cube(a) a*a*a

int a 2;

int i cube(++a);


the expression becomes

i (++a)*(++a)*(++a);

then finally a becomes 5 & i will be 125.

but the same in inline function gives value of i as 27.

because first value is processed & and then passed so the output will 27.



 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape