Difference between Inline and Macro in C++

Last Updated : 3 Jul, 2026

Inline functions and macros are both used to reduce the overhead of function calls by expanding code at the point of use.

  • Inline functions are handled by the compiler and provide type safety
  • Macros are processed by the preprocessor and perform simple text substitution.

Inline

An inline function is a function declared with the inline keyword that suggests the compiler replace the function call with its actual code. This helps reduce function call overhead for small functions.

  • Inline functions are expanded by the compiler and provide type checking like normal functions.
  • They are best suited for short and frequently called functions to improve performance.
C++
#include <iostream>
using namespace std;

// Inline function
inline int Maximum(int a, int b)
{
    return (a > b) ? a : b;
}

int main()
{
    cout << "Max (100, 1000):" << Maximum(100, 1000) << endl;
    cout << "Max (20, 0): " << Maximum(20, 0) << endl;

    return 0;
}

Output
Max (100, 1000):1000
Max (20, 0): 20

Syntax

inline return_type function_name ( parameters ) {

// inline function code

}

Explanation

  • inline: Suggests to the compiler that the function call should be replaced with the function's code to reduce function call overhead.
  • return_type: Specifies the type of value returned by the function, such as int, float, or void.
  • parameters: The input values accepted by the function. This part can be left empty if the function takes no arguments.

Macro

A macro is a preprocessor directive defined using the #define keyword. Before compilation, the preprocessor replaces every occurrence of the macro with its corresponding definition.

  • Macros perform simple text substitution and do not provide type checking.
  • They are commonly used to define constants, small code snippets, and conditional compilation directives.
CPP
#include <iostream>
using namespace std;

// macro with parameter
#define MAXIMUM(a, b) (a > b) ? a : b

int main()
{
    cout << "Max (100, 1000):";
    int k = MAXIMUM(100, 1000);
    cout << k << endl;

    cout << "Max (20, 0):";
    int k1 = MAXIMUM(20, 0);
    cout << k1;

    return 0;
}

Output
Max (100, 1000):1000
Max (20, 0):20

Syntax

#define MACRO_NAME Macro_definition 

Explanation

  • #define: A preprocessor directive used to define a macro before the program is compiled.
  • MACRO_NAME: The identifier used to represent the macro throughout the program.
  • Macro_definition: The value or code that replaces the macro name during preprocessing whenever it is encountered.

Inline Functions Vs Macro

The following table highlights the key differences between inline functions and macros in C++, based on their definition, behavior, debugging, argument evaluation, and usage.

Inline FunctionMacro
An inline function is defined using the inline keyword.A macro is defined using the #define preprocessor directive.
Inline functions can access class data members and behave like normal functions.Macros perform text substitution and do not have direct access to class members.
Inline functions are easier to debug because they are handled by the compiler.Macros are harder to debug since they are expanded before compilation.
Function arguments are evaluated only once.Macro arguments are expanded every time the macro is used, which may lead to unexpected results.
Inline functions can be defined inside or outside a class.Macros are typically defined before the program code using #define.
Short member functions defined inside a class are often treated as inline by the compiler.Macros must always be explicitly defined.
Inline functions are commonly used for small functions requiring type safety.Macros are widely used for constants, code snippets, and conditional compilation.
Inline functions are less common in competitive programming.Macros are frequently used in competitive programming for shorter code.
Inline functions end with a function body enclosed in {}.Macros end at the end of the line and do not require a terminating symbol.
Comment