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 : Define the term Class constant Define

    Define the term Class constant: A variable which is defined as both final and static.

  • Q : Double clock signal in synchronous

    Describes the cases where you need to double clock a signal before presenting this to a synchronous state machine?

  • Q : Explain Inheritance hierarchy

    Inheritance hierarchy: The relationship among super-classes and sub-classes is termed as an inheritance hierarchy. The single inheritance of classes means that each and every class has simply a single `parent' class and that Object class is the eventu

  • Q : What is an Implicit type conversion

    Implicit type conversion: The type conversion which does not need a cast. Implicit type conversions usually do not comprise any loss of information. For example, joining an integer operand with a floating point operand in an arithmetic expression will

  • Q : Explain Untyped Allocations Untyped

    Untyped Allocations: In C/C++ untyped allocations such as malloc, calloc, and realloc can easily be used to create overlays, which again require translation overhead to keep the corresponding non-overlaid objects consistent.

    Q : Define the term Module Module : It is a

    Module: It is a group of program components, usually with restricted visibility to program components in other modules. Java employs packages to implement this perception.

  • Q : Scripting Language Programming

    Hi Assignment Team, Please find attachment of my assignment. Please advise if there is any unclear information Regards, King

  • Q : Illustrations of XML DTDs or schemas

    Give some illustrations of XML DTDs or schemas which you have?

  • Q : Use of System Dynamic and System Runtime

    What is the use of System.Dynamic and System.Runtime.CompilerServices namespaces?

©TutorsGlobe All rights reserved 2022-2023.