Difference between macro and inline in c

Difference between macro and inline in c

Difference at Compile Time Expansion

Inline functions are similar to macros because they both are expanded at compile time, but the macros are expanded by the pre-processor, while inline functions are parsed by the compiler.


In above example, in case of macro expected output would be 25 but it will give you 11 because macros are substituted at compile time so SQAURE(X) would be like this 


and we know that in c operation priority goes to multiply operator i.e *, so 2*3 gives 6 and then addition of 2+6+3 becomes 11.

                      While in case of inline function argument is evaluated first and so x would be 5 which gives 5*5 = 25.

Difference at Argument Evaluation

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.



                             In this example, argument passed to macro will evaluate twice so output of macro function would be like 5 * 5 which gives 25 as a output while in case of inline function argument is evaluated only once so output would be 16.

Difference at Argument type checking

Inline follows strict parameter type checking, macros do not.


                               This is more specific clear case about argument data type checking, when we pass float argument to macro function it gives output properly i.e 1.21. But when we pass 1.1 to inline function whose argument type is int, it gives either compiler error or 0 as output. 

More about Macros and Inline

Macros are always expanded by pre-processor, whereas compiler may or may not replace the inline definitions. You cant force compiler to make a function inline. It is purely compiler based decision.

                           Debugging macros is also difficult. This is because the preprocessor does the textual replacement for macros, but that textual replacement is not visible in the source code itself. Because of all this, it’s generally considered a good idea to use inline functions over macros.

Suggested Reading

Interesting Facts about Macros and Preprocessors in C/C++