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,

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:





#include<stdio.h>
#define NUMBER 10
int main()
{
     printf("%d", NUMBER);
     return 0;

Output:

10


Difference between macro and functions 

MACROFUNCTION
Macro is PreprocessedFunction is Compiled
No Type Checking is done in MacroType Checking is Done in Function
Using Macro increases the code lengthUsing Function keeps the code length unaffected
Use of macro can lead to side effect at later stagesFunctions do not lead to any side effect in any case
Speed of Execution using Macro is FasterSpeed of Execution using Function is Slower
Before Compilation, the macro name is replaced by macro valueDuring function call, transfer of control takes place
Macros are useful when small code is repeated many timesFunctions are useful when large code is to be written
Macro does not check any Compile-Time ErrorsFunction checks Compile-Time Errors
Macros vs Functions

Macros are pre-processed which means that all the macros would be processed before your program compiles. However, functions are not preprocessed but compiledIn 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

Popular posts from this blog

What is a string ? Write a function in C for string concatenation. Without the use of inbuilt string function?

What is the scope of a Variable? Explain the difference between a global and local variables with example programs.

What is a pointer ? Write a C program using pointer to print the name and price of the items sold in a retail shop on a specific date.