EmbLogic's Blog

Difference Between Inline and Macro

Inline
An inline function is just like any other function in C++ and is also called in the regular way. The function it performs is that it creates a copy of the compiled function definition. That is, it creates a copy of the defined items to compile. An example can be taken if we are adding any two integers and call it the inline function, the compiler will create a copy of the integers to be compiled.
Example:

Inline int sum (int x, int y)
{
Return (x+y);
}

Macro
Macros in C++ implement text replacement in a program line. That is, they replace text according to the change defined in the function. Unlike inline as a function, a macro manipulates the code using a function. For example:
#define DOUBLE(X) X*X

int y = 5;
int j = DOUBLE (++y);

Here, we will get the value as 30! As the calling has been done via a macro, “X” has been replaced with ++y which makes ++y to be multiplied by another ++y. This makes a total of 5*6 that is 30 not 6. Six would be the basic but a wrong answer.

Now, macros might be causing a bug here. So an inline function comes to the rescue by copying the values to the compilers’ memory and then compiling it.

Summary:

1.An inline function creates a copy of the function definition.
2.A macro replaces the text as identified and defined within the function.
3.An inline function is also applied when a macro is supposed to cause a bug in the program.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>