Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming /Difference Between Macro and Inline Function

Difference Between Macro and Inline Function

April 5, 2018Posted byLithmee

Key Difference – Macro vs InlineFunction

A macro is a fragment of code, which is a preprocessor directive. An inline function is aC++enhancement feature to minimize the execution time of a program. Therefore, thekey differencebetween Macro and Inline Function is thata macro is checked by the preprocessor while an inline function is checked by thecompiler.

A macro is included at the beginning of the program preceded by a hash sign. When there is the macro name in the program, it is replaced by the content of the macro.

CONTENTS

1.Overview and Key Difference
2.What is Macro
3.What is Inline Function
4.Side by Side Comparison – Macro vs Inline Function in Tabular Form
5.Summary

What is Macro?

The preprocessor is a program that processes thesource codebefore it passes through the compiler. It operates using the preprocessor command line or the directives. In the program, the preprocessor directives are placed in the source program before the main program. Before the source code goes through the compiler, it is checked by the preprocessor for preprocessor directives. The preprocessor directives have the # symbol. Unlike other statements, they do not end with a semicolon. One type of preprocessor directive is macro. Generally, the macros are written in capital letters.

Difference Between Macro and Inline Function

Figure 01: C++ Program with Macros

According to the above program, line 3 and line 4 indicates macros. When calculating the area, the PI value is substituted using the defined macro. In line 14, volume= CUBE(value), the preprocessor expands the statement as volume = (value * value * value). Finding the cube can be written as a function, but in here it is written using a macro. If there is a statement as volume = CUBE(x+y), then it will expand to volume = (x+y * x+y * x+y).

Some programming tokens might easily be mistyped. They can be replaced using macros. e.g. #define AND &&, #define OR ||. A macro definition can also include expressions such as #define AREA 4*5.56.

What is Inline Function?

When a function is called, the compiler takes some time to execute it. If the function is not very complex, the programmer can convert the function to an inline function. Refer the below program.

Difference Between Macro and Inline Function_Figure 2

Figure 02: Function without Inline

The print_hello is a simple function. It prints the string “Hello” when the function is called. The execution time for that function is 0.187s. When using the inline keyword as follows, the execution time reduces to 0.064s.

Key Difference Between Macro and Inline Function

Figure 03: Inline Function

Therefore, by using the inline keyword, the execution time reduces. The inline functions might not work if there are loops, switch statements and if the function contains static variables or recursive functions.

What is the Difference Between Macro and Inline Function?

Macro vs Inline Function

A macro is a fragment of code, which is a preprocessor directive that is included at the beginning of the program preceded by a hash sign. An inline function is a C++ enhancement feature to minimize the execution time of a program.
Evaluation Time
In macro, the argument is evaluated each time it is used in the program. In inline, the argument is evaluated once.
Checked By
A macro is checked by the preprocessor. An inline function is checked by the compiler.
Keyword
Marco uses #define. The inline function uses the keyword ‘inline’.
Usage
Macro can be used to define constants, expressions, for literal text substitution and to define functions etc. An inline function can be used to minimize the execution time of the program.
Termination
Macro terminates with the new line. 内联函数终止的花括号the end of the inline function.
Defining Point
A Marco is defined at the beginning of the program. An inline function can be inside or outside the class.

Summary –Macro vs InlineFunction

This article discussed the difference between Macro and Inline Function. These concepts are used in C++ programming. The difference between Macro and Inline Function is that a macro is checked by the preprocessor while an inline function is checked by the compiler.

Reference:

1.“Preprocessor Directives.” Cplusplus.com.Available here
2.“C Inline Functions.” Www.tutorialspoint.com, Tutorials Point, 19 Mar. 2018.Available here

Related posts:

Difference Between Procedures and Functions in Programming Difference Between Abstract Class and Inheritance Difference Between Agile and V Methodologies (Model) Difference Between Netbeans and Eclipse Difference Between printf and fprintfDifference Between printf and fprintf

Filed Under:ProgrammingTagged With:Inline Function,Inline Function Checking,Inline Function Definition,Inline Function Evaluation Time,Inline Function Keyword,Inline Function Termination,Inline Function Usage,Macro,Macro and Inline Function Differences,Macro and Inline Function Similarities,Macro Checking,Macro Definition,Macro Evaluation Time,Macro Keyword,Macro Termination,Macro Usage,Macro vs Inline Function

About the Author:Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Natural and Organic

Difference Between Stabilizing and Balancing Selection

Difference Between Stabilizing and Balancing Selection

Difference Between Education and Experience

Difference Between Education and Experience

Difference Between Organ Specific and Tissue Specific Manifestations

Difference Between Organ Specific and Tissue Specific Manifestations

Difference Between Nap and Sleep

Difference Between Nap and Sleep

Latest Posts

  • 维生素和氨基酸之间的区别是什么Acids
  • What is the Difference Between Induction Cooker and Normal Cooker
  • What is the Difference Between Vitiligo and Psoriasis
  • What is the Difference Between Cancellous and Cortical Bone
  • What is the Difference Between Electric and Induction Cooktop
  • What is the Difference Between Prevention and Cure
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.