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 : Benefits of the event-delegation model

    Write down some of the benefits of the event-delegation model over event-inheritance model?

  • Q : Modeling Language for SPIN-Promela

    Modeling Language: The modeling language used for SPIN is called Promela (Process Meta Language). In fact, the name SPIN stands for Simple Promela Interpreter. Promela is a powerful C-like specification language with a variety of synchronization primi

  • Q : Is a XML replacing HTML Is a XML

    Is a XML replacing HTML?

  • Q : System Architecture for Windows

    Tell me about the System Architecture for Windows Programming?

  • Q : Explain the term Test Fusion Report of

    Explain the term Test Fusion Report of QTP?

  • Q : What is an Infinite recursion Infinite

    Infinite recursion: Recursion which does not finish. This can effect from any of direct recursion, indirect recursion or the mutual recursion. It is generally the outcome of a logical error, and can consequence in stack overflow.

  • Q : Define Heap Abstractions Heap

    Heap Abstractions: The class abstractions that we discussed above are obtained by abstracting each field of base type. The number of instances of that particular class still needs to be bounded; this results in an under-approximation that is still use

  • Q : What is Shallow copy Shallow copy : It

    Shallow copy: It is a copy of an object in which copies of each and every object's sub-components are not as well made. For example, a shallow copy of an array of objects would outcome in two separate array objects, each having references to similar s

  • Q : Define Stream class Stream class : An

    Stream class: An input stream class is one which delivers data from its source (frequently the file system as a series of bytes. Likewise, an output stream class will write byte-level data. The stream classes must be contrasted with the operation of r

  • Q : Explain Method overriding Method

    Method overriding: It is a method stated in a super class might be overridden by a method of similar name stated in a sub class. The two methods should have similar name and number and types of formal arguments. Any checked exception thrown by sub-cla

©TutorsGlobe All rights reserved 2022-2023.