Macros and Types of Macros Assignment Help

Assignment Help: >> C++ Programming >> Macros and Types of Macros

Macros

A macro is a piece of code which has been provided a name. At whatever time the name is employed, it is substituted by the contentednesses of the macro. There are 2 forms of macros. They vary  in most cases in what they appear when they are employed. Object-like macros correspond data objects when employed, function-like macros correspond function calls.

The #determine directive is by and large employed to comrade significant identifiers with constants keywords  ordinarilly employed statements or expressions. Identifiers which constitute constants are  in certain cases  denoted as manifest constants. Identifiers which constitute expressions or statements are denoted as macros.

When the term of the macro is distinguished in in the arguments of some other preprocessor commands or the program source text it is addressed as a call in to which macro. The macro name is interchanged by a copy of the macro body. If the macro assumes on arguments, the real arguments accompanying the macro name are deputized for conventional parametric quantity in the macro body. The procedure of replacing a macro call with the processed copy of the body is referred as expansion of the macro call.

There are 2 forms of macros. "Object-like" macros take no arguments, while on the contrary "function-like" macros could be determined to assume arguments thus which they act  and  look like function calls. As  macros do not bring forth actual function calls, programmer  could  in certain cases  make programs execute faster by replacing function calls with macros. In C++, inline functions are oftentimes a preferred method.

In whatever means , macros could produce issues if programmer  do not determine and employ them with proper attention. programmer  might bear to employ parentheses in macro definitions with arguments to go forward the proper priority in an expression. In addition, macros might not correctly deal expressions with drawbacks

As soon as programmer  bear outlined a macro, programmer  cannot delineate it to a dissimilar value without first eliminating the original definition. In whatever means , programmer  could delineate the macro with incisively the similar definition. Thus, the similar definition could come up more than at one time in a program.

The #undef directive eliminates the definition of a macro. As soon as programmer  bear got rid of the definition, programmer  could delineate the macro to a dissimilar value. The #determine Directive and The #undef Directive discuss the #undef  and  #determine directives in the order given.

C++ extends new capabilities, some of which supplant those extended by the ANSI C preprocessor. These new abilities enhance the predictability and  subdivision safety of the programming language :

 

In C++, objects declared as const could be employed in constant expressions. This permits programs to declare constants which bear subdivision  enumerations  and  value data, which could be considered in a symbolic manner with the debugger. Utilizing the preprocessor #determine directive to determine constants is not as accurate. No storage is apportioned for a const object excluding when an expression which acquires its deal is ascertained in the program.

 

The C++ inline function capableness supersedes function-subdivision macros. The benefits of employing inline functions across macros are:

ñ  Type safety. Inline functions are predicted to the similar subdivision assuring as normal functions. Macros aren't subdivision safe.

ñ  Correct dealing of arguments which bear draw backs. Inline functions assesses the expressions catered as arguments prior to entering the function body. Thus, there is no opportunity which an expression with drawbacks would be insecure.

 

Types of Macros are as accompanying:

Object-like Macros

An object-like macro is a simple identifier which would be substituted by a code piece. It is denoted as object-like as  it looks like a data object in code which employs it. They are most generally employed to give symbolic names to numeric constants.

 

Macro Arguments

Function-like macros could assume arguments, just like true functions. To determine a macro which employs arguments, programmer  insert parametric quantity among the pair of parentheses in the macro definition which make the macro function-like. The parametric quantity bear to be valid C identifiers, assorted by commas and optionally whitespace.

 

Stringification

Sometimes programmer  might want to convert a macro argument into a string constant. Parameters are not substituted inside string constants, but programmer  could employ the '#' preprocessing operator instead. When a macro parametric quantity is employed with a leading '#', the preprocessor substitutes it with the literal text of the actual argument, changed over to a string constant. In contrast to normal parametric quantity replacement, the argument is not macro-elaborated first. This is referred as stringification.

 

Concatenation

It is oftentimes significant to blend two tokens into one while expanding macros. This is denoted as token pasting or token concatenation. The '##' preprocessing operator executes token pasting. When a macro is elaborated, the two tokens on either side of every one considered individually '##' operator are aggregated into a single token, which then substitutes the '##' and the two original tokens in the macro expansion. In general, both would be identifiers or one would be an identifier and the other one would be  a preprocessing number. When pasted, they produce a longer identifier. This is not the valid event. There are likewise chances to add two numbers or a number and a name, such as 1.5 and e3 into a number. In addition, multi-character operators such as += could be organized by token pasting.

Object-like Macros

An object-like macro is a simple identifier which would be substituted by a code piece. It is denoted as object-like as  it looks like a data object in code which employs it. They are most generally employed to give symbolic names to numeric constants.

 

Importance of Macros:

The vantages of Macros is as accompanying:

1.    Macros could take 9 parametric quantity up to maximum.

2.    Macros are elaborated at generation or compiling.

3.    Macros make program interpretable.

4.    Macros are much faster as compared  to functions. Macros might make compiling slower but compiled programs are faster as functions  they comprise passing values which increase usage of cpu.

5.    They make the program compact.

6.    Determining arbitrary constants is easy.

7.    Macros could employed to shorten oftentimes employed commands or operations.

 

Weakness of Macros:

1.It make the program indecipherable, if programmer could employ too much in the program.

2. programmer  ought employ ansi c99's measure about macros.

 

Students can get solutions for  Macros in C++ computer programming online. ExpertsMinds interactive academic session will make learning Macros in C++ computer programming easy. Get answers online to all the questions, assignments, homework on Macros in C++ computer programming , under the expert guidance of the tutors. Expertsmind.com offers  Macros in C++ computer programming online tutoring service, Macros in C++ computer programming homework help and Macros in C++ computer programming anytime from anywhere 24x7.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd