OOAD Miscellanous Q/A #4


Question:What are macros? How macros are useful? Explain with examples.

Answer:

Macro can be defined as single identifier which is equivalent to expression or statements. Macro substitution is a process in which an identifier in a program is replaced by a pre-defined string. The pre defined string may consist of expression or a complete statement or a group of statements. For this task #define statement is used. This statement is known as macro. A macro has the following form:

#define identifier string.

The preprocessor replaces every occurrence of the identifier in the source code by the provided string. The definition of macro should start with the keyword # define and should be followed by an identifier and a string with at least one blank space between them. The string may be any text and identifier must be a valid name. There are different forms of macro substitution. The most common forms are following:

  1. Simple macro substitution - Simple string replacement is commonly used to define constants. For example,

    #define  pi 3.1415926
    

    This statement will replace all occurrences of identifier "pi" in the program with the value of 3.1415926. The statement # define pi 3.1415926 is called a macro. Writing macro definition in capital letters is a convention not a rule. A macro can include expression as well. Following are valid examples of a macro:

    #define AREA 14.26
    #define MAX 100
    
  2. Argument macro substitution - This macro substitution allows to define more complex and more useful forms of replacement. It takes the following form:

    #define identifier (f1, f2, f3, ...,fn) string
    

    where f1, f2, f3, ...,fn are the arguments. For example,

    #define CUBE (x) (x*x*x)
    

    If the statement value = cube (side); appears later in the program, it will be expanded by the preprocessor as:

    volume = (side *side *side);
    

    The following example defines max ( ) with arguments:

    #define MAX( a,b) ((a>b)?a:b)
    

    This macro is called like a function with two parameters.

    k = MAX (p,q); //is replaced by 
    k = (p>q)?p:q;
    
  3. Nested macro substitution - One macro can be used in the definition of another macro. That is macro definitions may be nested. The general form of this type of substitution is:

    # define identifier1 string1
    # define identifier2 (identifier 1, string 2)
    

    For example,

    # define  square (x) (x*x)
    # define cube (x) (square(x)*x)
    

    Thus the occurrence of statement cube (x); in the program will be replaced by (x*x*x)

Uses of Macros

Macros enables the programmer to create a name for a string and then use the name throughout the program. The advantage of such as macro is that it only needed to be modified once in the # define directive , and when the program is recompiled , all occurrences of the string in the program will be modified accordingly making writing the source code easier in big programs. Moreover , macros are useful in the following ways:

  • They increase readability of the program.

  • They make programs more portable.

  • They increase efficiency of the programs.

  • Using macros, it is very easy to make modifications in the program.

  • They increase the understandability of the program.

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements