Important Facts About MACRO in C

Macro is most powerful & dangerous resource of c language. If it used in the proper way then it can increase the readability and performance. Else it can cause the debugging issue which is difficult to solve.

Some of the fact about macro:

  • Macro is pre-processor directive.
  • Macro is used to define the constant variable.
  • Macro can be used as the replacement of small function to increase the speed of execution.
  • Macro will increase the size of the code.
  • Due to macro, a speed of execution will increase. 
  • Macro will increase the readability. 
  • Sometimes macro will give you re-usability. 
  • Alternative of macro is to use the const variable. 

It has many Advantages and Disadvantages. Before going to that part, we will just have a look to a simple macro definition.

Simple Macro Definition:

#define NO_OF_ITEMS 10

This definition will replace NO_OF_ITEMS with 10 incomplete code before compilation as it is pre-processor directive. 

Now let take different macro definition:

Textual Replacement 

#define SQUARE(x) (x * x)

Ideal function of this macro is to perform the square of the number. But now let's see why it can cause a problem. We will check its usage in different ways:

Example: 1
int b = 10;
a = SQUARE(b);    //This works fine. 

Example: 2
a = SQUARE(5+4);    // Don't give Square value of 9

Expand like this:
5+4 * 5+4 =
5+ 20 + 4 =
29

Now change the MACRO Definition to:

#define SQUARE(x) ((x) * (x))
Now try to access macro using:

Example: 2
a = SQUARE(5+4);    // Give Square of 9 //Works fine

As macro is doing textual replacement, it is always good practice to cover the each parameter with opening and closing braces in a macro to avoid any misleading error in output. It would not give you compilation error so sometimes it is very difficult to track back the error in a bigger code where you have many source files. 

Type Checking in MACRO:

There will be no type checking MACRO. You can pass any type of argument it will fail and crash run time if it can not handle such inputs in macro. 

To understand the behavior of macro syntax and behavior have a look at below code & output of the code:

Code Example:

#include <stdio.h>

#define MAX_1 10
#define MAX_2 {10} //No compilation Error
#define SQUARE_1(x) (x*x)
#define SQUARE_2(x) {x*x}
#define PRINT_1(str) {printf("%s\n",str);}
#define PRINT_2(str) printf("%s\n",str)
#define PRINT_3(x) printf("%d\n",x);
#define PRINT_4(x) (printf("%d\n",x);)
#define PRINT_5(x) (printf("%d\n",x))
#define PRINT_6(x) {printf("%d\n",x);}


int main() {
    // your code goes here
    int a = 5, b = 10;
    char ch = 3;
    float f = 4.1;
    long l = 300;
    double d = 200;
    
    PRINT_1("KAPIL");
    PRINT_2("THAKAR");
    
    SQUARE_1(5);
    //SQUARE_2(5); // Compilation Error
    
    //PRINT_1(SQUARE_1(5)); //Run time Error
    //PRINT_2(SQUARE_1(5)); //Run time Error
    PRINT_3(SQUARE_1(5));
    PRINT_3(SQUARE_1(3+2));
    
    PRINT_3(SQUARE_1(MAX_1));
    //PRINT_3(SQUARE_1(MAX_2)); //Compilation Error
    
    //PRINT_4(SQUARE_1(MAX_1));
/*
prog.cpp:36:2: note: in expansion of macro 'PRINT_4'
  PRINT_4(SQUARE_1(MAX_1));
  ^
prog.cpp:11:38: error: expected primary-expression before ')' token
 #define PRINT_4(x) (printf("%d\n",x);)
                                      ^
            */
      
    PRINT_5(SQUARE_1(MAX_1));  //Removed ; from PRINT_4        
    PRINT_5(SQUARE_1(a));  //Removed ; from PRINT_4  
    PRINT_5(SQUARE_1(b));  //Removed ; from PRINT_4  
    PRINT_5(SQUARE_1(ch));  //Removed ; from PRINT_4  
    PRINT_5(SQUARE_1(d));  //Removed ; from PRINT_4  
    PRINT_5(SQUARE_1(f));  //Removed ; from PRINT_4 
    PRINT_5(SQUARE_1(l));  //Removed ; from PRINT_4  
    
    
    return 0;
}

OutPut:

KAPIL THAKAR
25
11
100
100
25
100
9
0
536870912
90000

Related Post:


Removing Space from Given String & Different Methods to Count Number of Ones in Given Value

Booting Sequence for Boot Process in Linux

How to count number of 1 or set bits in a given number

What is Segmentation fault?

How to Access Private Data Members in C++ without using friend function

Process Vs Thread 

How to Add Two Numbers Without Using + Operator in C 

Overview of Function Pointer in C with Example - What is function pointer & how to use it

How to Fork child process with waitpid & execl in linux with c example code

Share on Google Plus

About Kapil Thakar

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...