Macro

How to create macro using #define is given below:
Already you have know how to assign a value in a variable. You can assign a value like this:
int a;
a=10;
You can assign a variable in other variable like this:
int a, b;
a=10;
b=a;
Here a variable is assigned in b variable through the line b=a; If you want to assign a mathematical equation, you should use #define keyword. Here is a mathematical problem:
f(x)=x^2-60x+4
Now we write a program to calculate f(x) for different value of x through #define keyword.

#include<stdio.h>
#define f(x)  (x*x -6*x +4)
int main ()
{
   float a, b;
  printf(" x = ? ");
  scanf("%f", &a);
  b = f(a);
  printf("f(x) = %f", b);
  return 0;
}

OUTPUT

x = ? 6
f(x) = 4.000000


Next Previous