Difference between inline and macro in C++

Hey, guys today we are going to learn about the difference between inline and macros in C++. Before starting with the difference lets discuss macro and inline first.

Macro in C++

Macros are statements or expressions which are processed before the compilation of program takes place and are thus called ‘preprocessor directives’. Macros begin with a ‘#define’ in their declaration. A macro assigns a name to statement or expression. The preprocessor replaces the name of a macro with its definition wherever it finds the name of the macro in the program.  Thus the macro is just the ‘text replacement’. Even the keywords can be defined as macros as the compiler detects the keywords, not the preprocessor. Syntax of a macro in C++ is

#define macro_name macro_expression

Note: Macros are not type-checked which can result in some unexpected error. Macros also evaluate the argument passed multiple times as it appears multiple times in definition which results in unexpected values.

Example of a macro :

#include <iostream>
//Macro named square 
# define Square(a) a*a
using namespace std;

int main()
{
    int x= 15;
    cout<<"square of x is "<<Square(x);
/* showing that macro is just the text replacement*/  
    cout<<"square of x++ is "<<Square(x++);
    return 0;
}

Output:

square of x is 225square of x++ is 240

The above example shows that preprocessor just replaces macro’s name with its definition. As Square(x++) returns 240(x++ * x++) instead of returning 256 i.e square of x++.

Inline functions in C++

Inline functions are somewhat similar to normal function. Rather than passing the control to the function during the function call and returning the control back to the place of calling, Compiler just copies the Inline function body at the place of calling the function. Thus inline functions save the time spent for searching the function executing it and returning the control back i.e the compiler skips the whole process of the function call stack.
Member functions of a class are by default inline but we can make any function inline by using keyword ‘inline’. Only small functions are made inline as an inline function increases the length of the program by copying the code multiple times i.e code redundancy.

inline return_datatype function_name() 
{
  function_body
}

An example of the inline function:

#include <iostream>
//creating inline function square
inline int square(int x)
{
    int sq=x*x;
    return sq;
}
using namespace std;

int main()
{
    int x =15;
    cout<<"square of x is "<<square(x);
    return 0;
}

Output:

square of x is 225

macros and inline function might appear the same but they have differences between them.

What are the differences between inline and macro in C++

  •  The preprocessor expands the macros whereas the compiler copies the inline function at the place of calling
  • Macros are always declared at the beginning of the program.  In contrast, inline functions can be declared anywhere in the program. Member functions of a class are by default inline.
  •  The keyword used to declare a macro is ‘#define’ whereas ‘inline’ is used for creating inline functions.
  •  Macros evaluate the passed argument multiple times when it appears at multiple places in the definition whereas the inline functions evaluate the passed argument only once.
  •  Macros are always expanded but inline functions are sometimes not expanded by the compiler i.e the compiler may ignore ‘inline’ and call the function as usual.
  •  Macro can access only the public data members of a class whereas the inline member functions can access all the data members of that class in which function is defined.
  •  Macros are usually single-lined and their declaration ends by a new line. In contrast, inline functions’ declaration is ended by ‘}’ just like normal functions.
  •  The compiler checks the code at the time of compilation and displays the error(if any). Thus errors in inline function are checked during compilation. In contrast, the errors of macros are not checked as they are preprocessed before compilation.
  • Also, macros are not type-checked whereas inline functions are type-checked.

Also, refer:

 

Leave a Reply

Your email address will not be published. Required fields are marked *