Difference between Inline and Macro in C++
Last Updated :
28 Jun, 2020
1. Inline :
An inline function is a normal function that is defined by the
inline keyword. An inline function is a short function that is expanded by the compiler. And its arguments are evaluated only once. An inline functions are the short length functions that are automatically made the inline functions without using the
inline keyword inside the class.
Syntax of an Inline function:
inline return_type function_name ( parameters )
{
// inline function code
}
Example of an Inline function:
CPP
#include <iostream>
using namespace std;
// Inline function
inline int Maximum(int a, int b)
{
return (a > b) ? a : b;
}
// Main function for the program
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
2. Macro :
It is also called
preprocessors directive. The macros are defined by the
#define keyword. Before the program compilation, the preprocessor examines the program whenever the preprocessor detects the macros then preprocessor replaces the macro by the macro definition.
Syntax of Macro:
#define MACRO_NAME Macro_definition
Example of Macro:
CPP
#include <iostream>
using namespace std;
// macro with parameter
#define MAXIMUM(a, b) (a > b) ? a : b
// Main function for the program
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
Difference between Inline and Macro in C++ :
S.NO |
Inline |
Macro |
1. |
An inline function is defined by the inline keyword. |
Whereas the macros are defined by the #define keyword. |
2. |
Through inline function, the class's data members can be accessed. |
Whereas macro can't access the class's data members. |
3. |
In the case of inline function, the program can be easily debugged. |
Whereas in the case of macros, the program can't be easily debugged. |
4. |
In the case of inline, the arguments are evaluated only once. |
Whereas in the case of macro, the arguments are evaluated every time whenever macro is used in the program. |
5. |
In C++, inline may be defined either inside the class or outside the class. |
Whereas the macro is all the time defined at the beginning of the program. |
6. |
In C++, inside the class, the short length functions are automatically made the inline functions. |
While the macro is specifically defined. |
7. |
Inline is not as widely used as macros. |
While the macro is widely used. |
8. |
Inline is not used in competitive programming. |
While the macro is very much used in competitive programming. |
9. |
Inline function is terminated by the curly brace at the end. |
While the macro is not terminated by any symbol, it is terminated by a new line. |
Similar Reads
Difference Between Inline and Normal Function in C++ Inline Function is a function that is expanded inline by the compiler when it is invoked. During function call, a lot of overhead tasks are performed like saving registers, pushing arguments to the stack, and returning to the calling function. These overheads are time-consuming and inefficient for s
4 min read
Difference Between STL and Standard Library in C++ In C++, the term "Standard Library" and "Standard Template Library" are often misinterpreted as the same. Although they sound same with only a single word difference, they refer to the different part of the C++ programming language. In this article, we will learn what's the difference between the C+
3 min read
Difference Between gcc and g++ The GNU Compiler Collection, abbreviated as GCC provides multiple compilers to compile source codes of different programming languages, mainly C and C++. In its command line interface, it provides two commands "gcc" and "g++" which are used to invoke the compiler to compile the given source code to
3 min read
Difference between virtual function and inline function in C++ Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class. Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments
2 min read
Difference Between Constant and Literals In this article, we are going to discuss the differences between Constant and Literals in a programming language with examples. ConstantsConstants are used to define values that remain fixed and cannot be changed during the execution of a program.In C++, they are typically declared using the const k
3 min read
Difference between Turbo C++ and Dev C++ 1. Turbo C++ (TC) : It is a compiling software used for C and CPP Language. The initial release was in May 1990 but the first stable version released in September 2008. It takes more memory and time to load as compared to Dev C++. Turbo C++ is the compiler from which most of us start our coding life
2 min read