Inline functions vs Preprocessor macros

difference between function and inline function in c
macro functions in c
static inline vs macro
inline function in c++
when to use macros in c
macro in c - geeksforgeeks
c macro function return
c++ macro function

How does an inline function differ from a preprocessor macro?

Inline functions vs Preprocessor macros, Inline replaces a call to a function with the body of the function, however, inline is just a request to the compiler that could be ignored (you could  inline functions are similar to macros (because the function code is expanded at the point of the call at compile time), inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor.

First, the preprocessor macros are just "copy paste" in the code before the compilation. So there is no type checking, and some side effects can appear

For example, if you want to compare 2 values:

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

The side effects appear if you use max(a++,b++) for example (a or b will be incremented twice). Instead, use (for example)

inline int max( int a, int b) { return ((a<b)?b:a); }

inline function vs macro function, Inline Functions. General introduction and comparison between macors and inline functions. +. Macros. Macros rely on textual substitution. The preprocessor​  An inline function is a C++ enhancement feature to minimize the execution time of a program. Therefore, the key difference between Macro and Inline Function is that a macro is checked by the preprocessor while an inline function is checked by the compiler. A macro is included at the beginning of the program preceded by a hash sign.

Preprocessor Macros V.S. Inline Functions · C++ Learning Note, One primary difference between inline and macro function is that the inline functions are expanded during Content: Inline Vs Macro. Inline functions can provide scope for variables, preprocessor macros can only do this in code blocks {…}, and static variables will not behave exactly the same way. The Inline function is expanded by the compiler whereas the macros are expanded by the Preprocessor , which is merely textual substitution.

The key difference is type checking. The compiler will check whether what you pass as input values is of types that can be passed into the function. That's not true with preprocessor macros - they are expanded prior to any type checking and that can cause severe and hard to detect bugs.

Here are several other less obvious points outlined.

Difference Between Inline and Macro in C++ (with Comparison , Macro vs. inline function. The behavior of a parameterized macro (a) and of an inline function (b) are very similar but there are some important differences. Inline functions are parsed by the compiler, whereas macros are expanded by the C++ preprocessor. This difference creates other differences, as best illustrated by examples. The C++ preprocessor implements macros by using simple text replacement.

To add another difference to those already given: you can't step through a #define in the debugger, but you can step through an inline function.

Macros and Inline Functions, Macros vs Functions. Macros are See the following example of Macro: There is a better way in modern compilers that is inline functions and const variable. Macro is an instruction which expands at the time of its invocation. Functions can also be defined, like macros. Similarly, the inline functions also expand at the point of its invocation. One primary difference between inline and macro function is that the inline functions are expanded during compilation, and the macros are expanded when the program is processed by the preprocessor.

Macros vs Functions, 9.4, How can inline functions help with the tradeoff of safety vs. speed? Unlike #define macros, inline functions avoid infamous macro errors since inline In other words, invoking an inline function is semantically just like invoking a regular​  Macro vs. inline function. The behavior of a parameterized macro (a) and of an inline function (b) are very similar but there are some important differences. The preprocessor expands macros by replacing the macro (e.g., "f(x,y)") with the statements in the macro body. The character or characters represented by a and b (including operators) replace x and y in the expansion.

Why should I use inline functions instead of plain old #define macros , Functions can also be defined, like macros. Similarly, the inline functions also expand at the point of its invocation. One primary difference between inline and  Macros are usually one liner. However, they can consist of more than one line, Click here to see the usage. There are no such constraints in functions. The speed at which macros and functions differs. Macros are typically faster than functions as they don’t involve actual function call overhead. Conclusion:

How does an inline function differ from a preprocessor macro?, The inline function-specifier was introduced to the C programming language in the C99 standard. Inline functions should be preferred over macros when they can  One other thing is that the macros are managed by preprocessor and inline functions are managed by C++ compiler. Remember: It is true that all the functions defined inside the class are implicitly inline and C++ compiler will perform inline call of these functions, but C++ compiler cannot perform inlining if the function is virtual.

Comments
  • Answers to stackoverflow.com/questions/132738/… contains some information related to your question.
  • Inline function are not always guaranteed to be inlined: Because the compiler will not inline if doing so will generate slower code etc. The compiler does a lot of analysis that the Engineer can not and does the correct thing.
  • I believe that recursive functions are another example where most compilers ignore inlining.
  • Are there any important differences in C compared to C++ in this case?
  • One point not mentioned is that inlining can be influenced by compilation flags. For example, when you build for maximum speed (like GCC -O2/-O3) compiler will choose to inline many functions, but when you build for minimum size (-Os) if will typically inline functions called only once (or very small functions). With macros there is no such choice.
  • Macros can't cover with access specifier (like, private or protected) while inline functions are possible.
  • Just want to add to your example that besides side effect, macro can also introduce extra work load, consider max(fibonacci(100), factorial(10000)) the larger one will get calculated twice :(
  • How much more fun do you get from #define TWO_N(n) 2 << n and then cout << CUBE(TWO_N(3 + 1)) << endl;? (It's better to end lines of output with endl than to begin them with it.)