Why Is A Macro Used In Place Of A Function?

Hello, this is Shivam and today I am going to tell you Why Is Macro Used In Place Of A Function? and we also discuss the all-term Macro What is Macro? Types of Macro and differences between the Macro And Function etc just be with me and enjoy the article.

What Is Macro And Its Types

Why Is A Macro Used In Place Of A Function?

Macro is a very important topic in c language we use Macro when we have to replace any name with code so macro is a segment of code that replaces the value with their name.

If we want to create any program of macro then we have to use (#define), which is a directive we use to define the macro.

Also, Read

Royal Enfield Y2 650 Price In India जाने क्या है ख़ास

Types Of Macro

There Are two types of macro

  • object type

In the object macro is used widely for the numeric value if we want to use any numeric value multiple times then we create a small macro name that will represent these numerical values.

When we need that numeric value when we simply call the macro name we have created then these name will represent the value

example:

#define A [10]

So the A is a macro name and the value 10 is a numeric term. if we call A then it will represent the 10

  • function macro

In the function macro is just like a function call In this macro it can substitute the code name with the function name means when we call the function then it will show the logic we have created in the function.

Why Is A Macro Used In Place Of A Function? And Difference

Speed and size The primary advantage of macros is quicker execution time. Each time a macro is used during preprocessing, its definition is inline expanded (replaced). No matter how many times a function is called, the definition only happens once. Although macros may lengthen the code, they do not carry the overhead of function calls.

Function assessment A macro does not evaluate an address; a function does. As a result, you cannot use a macro name in situations that call for a pointer. You cannot declare a pointer to a macro, but you can declare a pointer to a function.

Type-checking The compiler can examine the argument types when you declare a function. The compiler cannot check the types of macro argument since you cannot declare a macro; nonetheless, it may check the number of arguments you pass to a macro.