Macro Substitution Directives in C

Macro Substitution Directives in C Language with Examples

In this article, I am going to discuss the Macro Substitution Directives in C Language 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) Directives in C:

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 are required to use the 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 are required to use the CPP command. Cpp is an external command which is available in the 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 are required to create the “.i” file explicitly then it is 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 passed 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 the 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 successful then we will get an obj file, if linking is successful then we will get a .exe file. For the loading or execution of the program, we are required to use the application name or program name.exe.

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

Program to understand Macro Substitution Directives in C Language
#include <stdio.h>
#define size 120
void main ()
{
    int x;
    x = ++size;
    printf ("x=%d", x);
}
Output:

Program to understand Macro Substitution Directives in C Language

By using #define we can create a 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 it 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 that will be executed automatically before passing the source to the compiler

What are 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, 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 go for macro.

Advantages of Macro in C Language:
  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, the types checking process will not have occurred
Drawbacks of Macro in C Language:
  1. No syntactical problems can be considered at the time of pre-processing.
  2. Macros are 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 the binding procedure. Macros are under the control of a pre-processor which will execute automatically at the time of pre-processing.

Program by using Macros in C Language:
#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 Language:
#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 in C Language:
#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 in C

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 with Examples

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 Directive in C Language

In the next article, I am going to discuss File Inclusion Directives in C Language with Examples. Here, in this article, I try to explain Macro Substitution Directives in C Language with Examples. I hope you enjoy this Macro Substitution Directive in C Language with Examples 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 *