Macro Substitution Directives in C

Macro Substitution Directives in C

In this article, I am going to discuss the Macro Substitution Directives in C with Examples. Please read our previous article, where we discussed Pre-Processing in C. At the end of this article, you will understand what Macro Substitution Directives in C are and when and how to use Macro Substitution Directives in C Program with examples.

Macro substitution (#define):

When we are working with #define at the time of pre-processing where an identifier is occurred, which is replaced with the replacement text. Replacement text can be constructed using single or multiple tokens. A token is a combination of keywords, operators, separators, constants, or any other identifiers.

Syntax: #define identifier replacement_text

According to syntax, at least a single space must be required between #define, identifier, and identifier, replacement_text. When we are working with #define, it can be placed anywhere in the program but recommended to place on top of the program before defining the first function. By using #define, we can create symbolic constants which decreases the burden on the programmer when we are working with an array.

Designing a C program with DOS commands

For editing the program, we required to use edit command edit is an internal command which is available along with OS.

Syntax: edit filename.c
Example: D:\C1100AM>edit p1.C

Code in p1.C:
#define A 15
void main()
{
  int x;
  x=A;
  printf(“%d %d”,x,A);
}

//save p1.C (filesave)
//close p1.C (fileexit)
To process the pre-processing, we required to use the CPP command. Cpp is an external command which is available in c:\TC\BIN directory.

Syntax: CPP filename.c
Example: D:\C1100AM>CPP p1.C

Note: Pre-processing is an automated program that will be executed automatically before passing the source code to the compiler. If we required to create .i file explicitly then mandatory to perform.

void main()
{
 int x;
 x=15;
 printf(“%d %d”,x,15);
}

As per the above observation, at the time of pre-processing where an identifier A has occurred, it is replaced with the replacement text. No pre-processor related directives can be understandable to the compiler, that’s why all pre-processor related directives are removed from source code. .i file is called extended source code which is having actual source code that is passing to the compiler. For the compilation & linking process, we are required to use the TCC command. TCC is an external command which is available in C:\tc\Bin directory.

Syntax: TCC filename.c
Example: D:\C1100AM>TCC p1.C

When we are working with TCC command compilation & linking both will be performed at a time. If the compilation is success then we will get an obj file, if linking is success then we will get .exe file. For the loading or execution of the program, we required to use the application name or program name.exe.

Syntax: program name.exe
Example: D:\C1100AM>p1.exe
Example: D:\C1100AM>p1

Program
#include <stdio.h>
#define size 120
void main ()
{
    int x;
    x = ++size;
    printf ("x=%d", x);
}

Output:

Macro Substitution Directives in C

By using #define we can create symbolic constant value (i.e. at the time of pre-processing actual data will be substituted) which is not possible to change at the time of execution.

Program
#define A 2+3
#define B 4+5
int main ()
{
    int c;
    c = A * B;
    printf ("c =  %d", c);
    getch ();
    return 0;
}

Output: C = 19
Logic
C = A * b
= 2 + 3 * 4 + 5
= 2 +12+5
= 19

Note: In implementation when we are composing the replacement text with more than one token then always recommended to place within the parenthesis only.

Program:
#include <stdio.h>
#define A (2+3)
#define B (4+5)
void main ()
{
    int c;
    c = A * B;
    printf ("c=%d", c);
}

Output: c=45

Note: pre-processing is an automatic program which will be executed automatically before passing the source to the compiler

What is an internal command and external command?
  1. The commands which are already available in the operating system are called internal commands.
  2. Internal commands are path independent commands, i.e. from any specific location those commands can be executable.
  3. After installing any specific software if we are getting the commands then it is called external commands.
  4. External commands are path-dependent commands i.e. which specific location it is installed, in the same location it works.
What is Macro in C?

The simplified function is called Macro. When the function body contains 1 or 2 statements then it is called a simplified function. In implementation whenever the simplified function is required than in place of creating the function it is recommended to for macro.

Advantages of Macro:
  1. Macros are faster than normal functions.
  2. No physical memory will be occupied when we are working with macros.
  3. When we are working with macros, code substitution will happen in place of the binding process.
  4. In macros, types checking process will not have occurred
Drawbacks of Macro:
  1. No syntactical problems can be considered at the time of pre-processing.
  2. Macros required to construct in a single line only.
  3. No type checking process has occurred, when we are working with macros (parameter checking process).
  4. No control flow statements are allowed.
  5. A return statement cannot be placed in a macro.
  6. In macros, no compilation errors will check.
Program:
#include<stdio.h>
int sum (int x, int y)
{
    return (x + y);
}

void main ()
{
    int s;
    s = sum (10, 20);
    printf ("sum value is %d", s);
}

Output: sum value is 30

In the above program at the time of pre-processing where sum macro has occurred, it is replaced with replacement text automatically at the time of pre-processing. When we are working with the function it executes at the time of the compilation process with the help of binding procedure. Macros are under the control of pre-processor which will execute automatically at the time of pre-processing.

Program by using macros:
#include<stdio.h>
#define sum(x,y) x+y
void main ()
{
    int s;
    s = sum (10, 20);
    printf ("sum value is %d", s);
}

Output: sum value is 30

In the above program at the time of pre-processing when we are calling sum macro, automatically it is replaced with the replacement text.

Program:
#include<stdio.h>
int max (int x, int y)
{
    if (x > y)
        return x;
    else
        return y;
}

void main ()
{
    int m;
    m = max (10, 20);
    printf ("max value is %d", m);
}

Output: max value is 20

Program by using macro in C:
#include<stdio.h>
#define max(x,y) x>y?x:y
void main ()
{
    int m;
    m = max (10, 20);
    printf ("max value is %d", m);
}

Output: max value is 20

Program using the nested macro:
#include<stdio.h>
#define SQR(a) (a)*(a)
#define CUBE(a) SQR(a)*(a)
void main ()
{
    int i;
    i = CUBE (2 + 3);
    printf ("i= %d", i);
}

Output: i= 125

What is the output from the below program?
#include<stdio.h>
#define SQR(a) a*a
int main ()
{
    int i, j;
    i = SQR (2);
    j = SQR (2 + 3);
    printf ("i =  %d j = %d", i, j);
    return 0;
}

Macro Substitution Directives Examples in C Language

What will be the output from the below program?
#include<stdio.h>
#define SQR(a) (a)*(a)
int main ()
{
    int i, j;
    i = SQR (2);
    j = SQR (2 + 3);
    printf ("i =  %d j = %d", i, j);
    return 0;
}

Macro Substitution Directives in C Language

What will be the output from the below program?
#include<stdio.h>
#define CUBE(a) (a)*(a)*(a)
int main ()
{
    int i, j;
    i = CUBE (2);
    j = CUBE (2 + 3);
    printf ("i =  %d j = %d", i, j);
    return 0;
}

Macro Substitution Directives in C

In the next article, I am going to discuss File Inclusion Directives in C language. Here, in this article, I try to explain Macro Substitution Directives in C. I hope you enjoy this Macro Substitution directive in C article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *