Understanding Macros In C

Macros are the piece of code that meaning has been defined to the compiler. Here C pre-processor takes place like include directive, It tells the meaning of macro to compiler. 

C provides its own pre-defined macros that can be used anywhere in the program. Programmer can also create own macros. 

C pre-processor defines  the macro,using define keyword and macro could be used as a constant or functions.

#define Macro_Name  
it could be any constant value, function call or any conditional macro. Let's see an example of most used macros.

#include <stdio.h>

/* Use of pre-defined macros
   __DATE__
   __TIME__
   __LINE__
   __FUNCTION__

*/


int main(void) {
 

printf("Current Date is %s\n",__DATE__);


printf("Current Time is %s\n",__TIME__);


printf("Current Line no. is %d\n",__LINE__);


printf("Current Function is %s\n",__FUNCTION__);


  return 0;

}


Output -


Current Date is Aug 14 2019

Current Time is 11:46:17

Current Line no. is 9

Current Function is main
These all are already defined, Let's create our own macros.

Constant Macro -  

#include <stdio.h>


// success macro definition
#define SUCCESS return 0


// failure macro definition
#define FAILURE return -1


int main(void) {
 
float a,b,c;

printf("Enter value of a and b ");

scanf("%f %f",&a,&b);


if(b == 0){
  
  printf("OOPS! Divisor can't be 0");

  FAILURE;

}

c = a/b;

printf("\nThe ans is %f", c);

SUCCESS;

}
Function Macro -  

#include <stdio.h>


//Constant macro
#define PI 3.14


//Function macro, calculates area of circle
#define circle_area(r) (PI*r*r) 

/*Function macro, calculates area of square
  Note: Macro name is not case sensitive
*/
#define SQUARE_AREA(a) (a*a) 


int main(void) {
 

float radius;

int side;


printf("Enter circle's radius ");

scanf("%f",&radius);

printf("\nThe area of circle is -%f", circle_area(radius));


printf("\n\nEnter square's side ");

scanf("%d",&side);

printf("\nThe area of square is -%d", SQUARE_AREA(side));


}
Conditional Macro Directives  -  
It is same as decision making statements in C. If we want to apply conditions over macros then there are some conditional directives available.


1. #ifdef - #else and #endif 

- Checks, if the macro name is already defined 
  in file or in included file.


#include <stdio.h>


#define VALUE 30


int main(void) {
 

#ifdef VALUE


 printf("Macro is already available");


#else


 printf("Macro is not available");


#endif  


return 0;

}

  
  Similarly #ifndef - #else and #endif 

- Checks, if the macro name is not already defined 
  in file or in included file.   


2. #if - #else - #endif

- Checks, conditional expression with Macro.


#include <stdio.h>


#define VALUE 30


int main(void) {
 

#if VALUE > 10


 printf("Value is > then 10");


#else

 printf("Value is < 10");


#endif  


return 0;

}  
  
 
3. #if - #elif - #else - #endif

- Checks, multiple conditional expression with Macro.


#include <stdio.h>


#define VALUE 30


int main(void) {
 

#if VALUE > 30


 printf("Value is > then 30");


#elif VALUE > 10 && VALUE <= 30


 printf("Value is > 10 but < 30");


#else


 printf("Value is < 10");


#endif  


return 0;

}  
   
Disadvantage of Using Macro -

As you could see that Macro provides fast programming and processing technique but macros are defined by pre-processors and macro is processed before the compilation so that it increase the program size. So it is recommended to use limited number of macros with applications.

Thanks for reading, do share with your friends and comment your suggestion.

No comments

Powered by Blogger.