Home » 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
int main()
    int num1,num2;
    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));
    return 0;


Enter first number : 10 
Enter second number: 20 
Sum is = 30 
Averageis = 15
Largest Number is = 20

Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.