What is the difference between a function and a macro? Find the largest number among two numbers using a function definition as well as a macro. Which is more efficient in terms of execution time and code size? 10m Jun2006

By | June 13, 2014

What is the difference between a function and a macro? Find the largest number among two numbers using a function definition as well as a macro. Which is more efficient in terms of execution time and code size? 10m Jun2006

 Difference between a function and a macro:

  Macro_VS_Function

Function Code

int MAX(X,Y)
{
   if(X<Y)
      return Y;
   else
      return X;
}

 Macro Code

#define MAX(X,Y) (X>Y ? X:Y)

Function takes 5 to 7 Lines where as Macro code is written in a single Line. As we discussed the execution time for macro is less, because No transfer of Control, less number of comparisons, and Number of lines less so less time needed Hence Macros are Efficient in this scenario.

 For More Details See:

 Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-that-uses-macros-min-max-to-find-and-return-respectively-the-minimum-maximum-of-two-values

 

 

Leave a Reply