Macro in C Programming - Complex macro with arguments in c
Learn C - C tutorial - complex macro with arguments in c - C examples - C programs
Complex macro with arguments in c
- Macros that accept arguments appear to be functions.Arguments are not typed as in a C function.
- Macros with arguments must be defined using the #define directive before they used.Argument list is enclosed in parentheses and must immediately follows the macro name.
Sample Code
#include<stdio.h>
#define SUM(A,B)(A+B)
#define AVG(A,B)((A+B)/2)
#define MAX(A,B)((A>B)?A:B)
int main()
{
int num1=8,num2=5;
printf("\nSum is = %d\n",SUM(num1,num2));
printf("\nAverage is = %d\n",AVG(num1,num2));
printf("\nLargest Number is = %d\n",MAX(num1,num2));
return 0;
}
Output
Sum is = 13
Average is = 6
Largest Number is = 8