Difference between Macro and Inline function in C++

By | February 25, 2024

1. Inline :
It is normal function that is inline keyword and expanded by the compiler. Its arguments are evaluated only once.

Syntax of an Inline function:

inline return_type function_name ( parameters )
{ 
 // inline function code
} 

2. Macro :
It is also called preprocessors directive and these are defined by the #define keyword.

Syntax of Macro:

#define MACRO_NAME Macro_definition 

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++). That's why we use inline function.

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

Macro Vs Inline function

Difference between Macro and Inline function C++ :

S.No. INLINE MACRO
1. Inline functions are parsed by the compiler. Macros are expanded by the preprocessor.
2. These are defined by inline keyword. These are defined by #define keyword.
3. These can be defined inside or outside the class. These are always defined at the start of the program.
4. Error checking is done during compilation, so debugging is easy. There is no error checkiong during compliation, so debugging is not easy.
5. Argument evaluated only once. Argment evaluated each time used in the code.
6. Inline member function can access data members of class. It cannot be memmber of any class so cannot access data menmber of class.
7. It terminates with the curly brackets at the end of the inline function. It terminates with the new line.
8. These may not expand. These are always expanded.
9. There always type checking during compliatation. There is no type checking.
10. Syntax:
inline return_type funct_name ( parameters ){ . . . }
Syntax:
#define macro_name char_sequence



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.