Macro with Arguments

Now we are going to learn about Macros with arguments.They will short program very much and it will increase readability of the program.Now Let us see example for it.If you don't now what is macro the just click here to learn about macro.Now let us see how to define macro with argument.

#define NAME(variable_to_use) (operation)

Now let us see that how much it is effective by creating simple squaring function.

First by creating function:

#include<stdio.h>
#include<conio.h>
int square(int);
main()
{
    int a=5,ans;
    clrscr();
    ans=square(a);
    printf("square of %d is %d",a,ans);
    getch();
}
int square(int x)
{
    int ans;
    ans=x*x;
    return ans;
}


then by normal method

#include<stdio.h>
#include<conio.h>
main()
{
    int a=5,ans;
    clrscr();
    ans=a*a;
    printf("Square of %d is %d",a,ans);
    getch();
}


then at last by creating macro with argument

#include<stdio.h>
#include<conio.h>
#define square(x) x*x
main()
{
    int a=5,ans;
    clrscr();
    ans=square(a);
    printf("\nSquare if %d is %d",a,ans);
    getch();
}


it looks like a program created with the help of macro is more readable then created by other methods.A normal method is easy for this program but may be harder for other programs.So, we have seen that by using macro how we can create a more readable programs.