Macros are pre-processed which means that all the macros would be processed before your program compiles. However, functions are not preprocessed but compiled.
See the following example of Macro:
See the following example of Function:
Now compile them using the command:
gcc –E file_name.c
This will give you the executable code as shown in the figure:
This shows that the macros are preprocessed while functions are not.
In macros, no type checking(incompatible operand, etc.) is done and thus use of micros can lead to errors/side-effects in some cases. However, this is not the case with functions. Also, macros do not check for compilation error (if any). Consider the following two codes:
Output: Unexpected output
Output: As expected
- 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.
Macros are no longer recommended as they cause following issues. There is a better way in modern compilers that is inline functions and const variable. Below are disadvantages of macros:
a) There is no type checking
b) Difficult to debug as they cause simple replacement.
c) Macro don’t have namespace, so a macro in one section of code can affect other section.
d) Macros can cause side effects as shown in above CUBE() example.
|Macro is Preprocessed||Function is Compiled|
|No Type Checking is done in Macro||Type Checking is Done in Function|
|Using Macro increases the code length||Using Function keeps the code length unaffected|
|Use of macro can lead to side effect at later stages||Functions do not lead to any side effect in any case|
|Speed of Execution using Macro is Faster||Speed of Execution using Function is Slower|
|Before Compilation, macro name is replaced by macro value||During function call, transfer of control takes place|
|Macros are useful when small code is repeated many times||Functions are useful when large code is to be written|
|Macro does not check any Compile-Time Errors||Function checks Compile-Time Errors|
See following for more details on macros:
Interesting facts about Macros and Preprocessors
This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- X-Macros in C
- Macros and its types in C/C++
- Multiline macros in C
- Importance of macros in C++
- Predefined Macros in C with Examples
- Hygienic Macros : An Introduction
- Branch prediction macros in GCC
- Output of C++ programs | Set 25 (Macros)
- Data Type Ranges and their macros in C++
- Interesting Facts about Macros and Preprocessors in C
- Output of the Program | Use Macros Carefully!
- Variable length arguments for Macros
- Functions in C/C++
- Searching in a map using std::map functions in C++
- C++ Mathematical Functions
- Nested functions in C
- List in C++ | Set 2 (Some Useful Functions)
- Thread functions in C/C++
- Pure Functions
- Functions that cannot be overloaded in C++
Improved By : RishabhPrabhu