What is a macro ? Explain how a macro is defined in C. Also explain major differences between a macro and a function. Explain a situation when macro should be prefered over function with an example.
What is a macro? Explain how a macro is defined in C. Also explain major differences between a macro and a function. Explain a situation when macro should be preferred over function with an example?
IN C language,
A macro is a fragment of code that has been given a name. Whenever the name is used, it is replaced by the contents of the macro.
There are two kinds of macros. You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords. macro is preprocessed.
When you use a MACRO, the C preprocessor will translate all strings using macro and then compile.
See the following example of Macro:
Output:
10
Difference between macro and functions
MACRO | FUNCTION |
---|---|
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, the 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 |
Macros are pre-processed which means that all the macros would be processed before your program compiles. However, functions are not preprocessed but compiled. In macros, no type of checking is done, so it may occur some problems for different types of inputs. In the case of functions, this is not done. Also for macros, if the inputs are not properly maintained, then it may generate some invalid results. Please check the following program to get an idea about the problem.
Example
#include <stdio.h>
#define SQUARE(x) x * x
int sqr(int x) {
return x*x;
}
main() {
printf("Use of sqr(). The value of sqr(3+2): %d\n", sqr(3+2));
printf("Use of SQUARE(). The value of SQUARE(3+2): %d", SQUARE(3+2));
}
Output
Use of sqr(). The value of sqr(3+2): 25
Use of SQUARE(). The value of SQUARE(3+2): 11
The function and macro, we want both will do the same task, but here we can see that the output is not the same. The main reason is when we are passing 3 + 2 as a function argument, it converts into 5, then calculate 5 * 5 = 25. For macro it is doing 3 + 2 * 3 + 2 = 3 + 6 + 2 = 11.
So the macros are not recommended for the following problems −
There is no type checking
Default to debug, as they cause simple replacement
Macro doesn’t have the namespace. So if the macro is defined in one section, it can be used in another section.
Macro increases the code length as it is added before the code while preprocessing.
Macro does not check any compile-time errors.
What do you think?
I hope, now you have a better understanding of MACROS Comments and suggestions regarding this article are welcome.
Comments
Post a Comment