C macro taking arguments

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Pre-processor Macro taking Argument : Argumented Macro (Macro having Arguments)

  1. Everytime whenever the macro name is encountered , the arguments are replaced by the actual arguments from the program.
  2. Macroname having Arguments is called Argumented Macro.
  3. Argumented macro is as called as function macro as it looks like calling function.

Advantages of Argumented macro :

  1. Arguments are not Case sensitive .
  2. Numeric macro Executes faster than the function

Example of Macro Name:

#define SQU(x) (x*x)

Example :

#include<stdio.h>
#define SQU(x)(x*x)

int main()
{
int x;
float y;

x = SQU(3);
y = SQU(3.1);

printf("
Square of Integer : %d",x);
printf("
Square of Float : %f",y);

return(0);
}

Output :

Square of Integer :  9
Square of Float   :  9.610000

Conclusion :

  1. We can pass any Numeric Parameter eg int,float,double
  2. So that Argumented macro is not case sensitive

Argumented Macro used for different purpose :

#define LARGE(x,y)((x)>(y)?(x):(y))
#define SQU(x)(x*x)
#define COM(s1,s2)(strcmp((s1),(s2))==0)