Macro | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Macro

What is difference between Macro and inline function . which one is better. Tell the case if both have there own benefits . Is it possible to use both of them in each kind of task i.e. can we interchange them as required ?

25th Mar 2017, 6:59 PM
Sahaj Bamba
Sahaj Bamba - avatar
3 Answers
+ 3
Macro is created using #define statement. When it called, the calling is replaced with the expression represented by that macro at compile time example #define sqr (x) x*x int main (){ cout << sqr (4); // results as cout <<(2*2) } Macro is datatype independent which is an advantage but more importantly is disadvantage as results in to inprecedent results in case of incompatible datatype. On the other hand, inline is a better feature then macro though it works on the same line as of macro with the difference that it is tightly glued with datatype. Moreover, it's structure matches standard UDFs. example inline long sqr (long x) { return (x*x); } int main (){ cout << sqr (4); // results as cout <<(2*2) } So inline functions are better than macros. Alternatively, you can also dig into function templates for adding more flexibility, elegance and spices to you coding as it may your code more generic.
25th Mar 2017, 7:30 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
+ 1
@Devender https://code.sololearn.com/cwlC9yJt42tE/?ref=app please take a look at this code and explain the errors .
25th Mar 2017, 8:03 PM
Sahaj Bamba
Sahaj Bamba - avatar
0
please see comment section in your code
25th Mar 2017, 8:11 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar