Macro definition in C and C++

Macro in C: Macros are defined as single identifiers that are equivalent to expressions, complete statement or groups of statements. Macros signify functions in this sense. They are defined in an all together different members than functions. Though, they are treated in a different way during the compilation procedure.

 

Consider the simple c programme

# include < stdio. h >

# define area length * width

Main ()

{

Int length, width;

Print f ("length = ")

Scan f (" % d" & length);

Print f ("width =")

Scan f ("% d", & width);

Print f ("area = % d", area);

}

This programme contains the macro area which represents the expression length & expression and width. When the programme is pile up, the expression length * width will swap the identifier area in the print statement in order that the print f statement will turn out to be

Print f ("area = % d, length * width);

Note the string "area % d" is unaffected by the # define statement. Macro definitions are usually placed at the start of a file, ahead of the first function definition. The scope of a macro definition widen from its point of definition to the ending of the file. A macro defined in one file is not known in other file. Multiline macros can be defined by placing a back ward slash at the end of each line except the last. This feature permits to a single macro (single identifier) to represent a compound statement. Here is another simple C programme that contains a macro:

This programme contains a multiline macro, which represents a compound statement.

Macro in C++: The programme will replace all the macro functions used in programme by their function body before the complication. The feature of macro functions is that there will be no extent function call during execution. Since the function body is substituted at the point of macro call during complications. Thus the runtime overhead for function linking or context switch time is decreased. The macro function spans for a maximum of one line only. 

   Related Questions in Programming Languages

  • Q : Describe Layout manager Layout manager

    Layout manager: The object responsible for sharing the accessible space between multiple components in a graphical container.

  • Q : Programming analysis and design

    Illustrate the term programming analysis and design in brief.

  • Q : Describe Method Method : The portion of

    Method: The portion of a class definition which implements some of the behavior of objects of the class. The body of the method includes declarations of local variables and statements to execute the behavior. The method receives input through its argu

  • Q : Block Started by Symbol What is the

    What is the Block Started by the Symbol?

  • Q : Define Accessor method Accessor method

    Accessor method: A technique specifically designed to offer access to a private attribute of a class. By convention, we name accessors with a get prefix followed by the name of the attribute being accessed. For example, the accessor for an attribute n

  • Q : What is no-arg constructor no-arg

    no-arg constructor: It is a constructor which takes no arguments. By default, each and every class without an explicit constructor has a default no-arg constructor with the public access. Its role is entirely to invoke the no-arg constructor of the in

  • Q : Define the term Interface inheritance

    Interface inheritance: Whenever a class implements an interface, the interface inheritance relationship exists among them. The class inherits no implementation from interface, just method signatures and static variables. It is as well possible for one

  • Q : Define the Binding of Instructions and

    Define the Binding of Instructions and Data to Memory?

  • Q : Libraries involved in Windows

    Explain the important libraries involved in Windows programming?

  • Q : Application of S60 device The

    The application has been earlier tested along with an S60 2nd Edition device and this is Symbian Signed. So can I install similar application to another S60 device?

©TutorsGlobe All rights reserved 2022-2023.