+ 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 ?
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.
+ 1
@Devender
https://code.sololearn.com/cwlC9yJt42tE/?ref=app
please take a look at this code and explain the errors .
0
please see comment section in your code