Example 1
#include <stdio.h>
#define SUM a+b // Macros used just to replace values .
// Whenever compiler see 'SUM' keyword in the program
// it will replaced with a+b .
// It works on the principal of function, so you don't have to
// write same long particular statement every where in the program
int main (){
int a = 5,
b = 3,
c;
c = SUM - 1; // this statement is same as c = a + b - 1;
printf("%d", c);
return 0;
}
Example 2
#include <stdio.h>
#define SUM(a,b) (a+b) // Macros look for each and every word or symbol
// So, here SUMa,b would not work.
#define MAX(a,b) (((a)>(b))?(a):(b)) // Also, it is not different than..
// MAX(a,b) (a>b ) ? (a:b)
// However, they are placed in brackets to
// avoid mathematical errors in complex equations
int main (){
int x = 5,
y = 3,
z, h;
z = SUM(x,y) - 1; // same as functions.. x => a and y => b
// SUM(x --> a, y--> b) = SUM (a,b)..
// replace with a + b ==> x + y
printf("%d",z);
h = MAX(x+y,z*x); //i.e. MAX (x + y --> a, z * x --> b)
MAX (8, 35) ==> (8>35) ? (8:35)
printf("%d",h); // of course statement is false so .. 35 is returned
return 0;
}
#include <stdio.h>
#define SUM a+b // Macros used just to replace values .
// Whenever compiler see 'SUM' keyword in the program
// it will replaced with a+b .
// It works on the principal of function, so you don't have to
// write same long particular statement every where in the program
int main (){
int a = 5,
b = 3,
c;
c = SUM - 1; // this statement is same as c = a + b - 1;
printf("%d", c);
return 0;
}
Example 2
#include <stdio.h>
#define SUM(a,b) (a+b) // Macros look for each and every word or symbol
// So, here SUMa,b would not work.
#define MAX(a,b) (((a)>(b))?(a):(b)) // Also, it is not different than..
// MAX(a,b) (a>b ) ? (a:b)
// However, they are placed in brackets to
// avoid mathematical errors in complex equations
int main (){
int x = 5,
y = 3,
z, h;
z = SUM(x,y) - 1; // same as functions.. x => a and y => b
// SUM(x --> a, y--> b) = SUM (a,b)..
// replace with a + b ==> x + y
printf("%d",z);
h = MAX(x+y,z*x); //i.e. MAX (x + y --> a, z * x --> b)
MAX (8, 35) ==> (8>35) ? (8:35)
printf("%d",h); // of course statement is false so .. 35 is returned
return 0;
}