C programming language
Complex macro with arguments (function like macro) in C language
Here, we will learn how we can define a complex macro which looks like a user define function in c programming language?
We can also define a macro with arguments, so that a macro may use at different places with different values. These values pass in the macro definition with the help of arguments.
Arguments must be used each time of macro calling.
This type of macro seems like a function; hence we can say that this is the "Function Like Macro Definition".
#define macro_name(argument_list) code_fragment
- #define is a pre-processor directive
- macro_name is the name of macro
- argument_list is the list of arguments
- code_fragment is the statement which is compiled at the place of the macro
Consider the following statement (as example code of complex macro)
#define SUM(A,B) (A+B)
Here, SUM is complex macro where A and B are the input arguments and the statement (A+B) is the code fragment which. At the time of compilation, compiler will replace the macro SUM with its code fragment (A+B).
C program demonstrating example of complex macro
#define SUM(A,B) (A+B) // addition of two numbers
#define AVG(A,B) ((A+B)/2) // average of two numbers
#define MAX(A,B) ((A>B)?A:B) // largest number
printf("Enter first number : ");
printf("Enter second number: ");
printf("Sum is = %d\n",SUM(num1,num2));
printf("Average is = %d\n",AVG(num1,num2));
printf("Largest Number is = %d\n",MAX(num1,num2));
Enter first number : 10
Enter second number: 20
Sum is = 30
Averageis = 15
Largest Number is = 20
Most viewed pages
- My personal website.
Keywords (like Linux, Java, Oracle etc) used in this website are the trademarks of their respective owners/company. This website is not affiliated to any company. The tutorials are designed by includehelp Team with the help of well known books and other online resources. These Tutorials are free for all, for learning purposes only. Enjoy learning:)
Copyright © 2015 - 2016. All Rights Reserved.