πΈMacros
a macro is essentially a piece of code based in the #define directive. a text processing feature and are expanded similar to normal functions.
Last updated
a macro is essentially a piece of code based in the #define directive. a text processing feature and are expanded similar to normal functions.
Last updated
example:
β’ all macros are preprocessed which means all of them would be processed before your program compiles. functions are not preprocessed, they are compiled.
β’ a macro is always faster than a function, functions take longer than inline code (macros for example).
β’ for one time use a macro is not really a big deal but a macro inside a nested loop is a much better candidate for speed improvements. we can use profilers to determine where a program spends the most time.
β’ when calling a function it has to a,locate some data (a newly allocated stack frame). macros don't have this overhead. macros insert directly into the program (textual program) so if we use the same macro 20 times we get 20 lines of code added into our program. functions are preferred over macros when writing large chunks of code
β’ with macros you don't have to worry about variable types
β’ functions give us type checking. if a function expects a string but you give it an int, you will get an error.
β’ debugging a macro is much harder than debugging a function. a function can be stopped through by the debugger but a macro cannot.
inline functions are the best alternative to macros. when we add the inline keyword in front of the function it hints the compiler to embed the function body inside the caller (just like a macro).
inline functions can be debugged and also have type checks.however the inline keyword is just a hint and not a strict rule.
example:
defines a macro and some replacement text. after definition the macro can be used as follows:
leading or trailing white space around the replacement text is discarded
example:
example:
defines a macro and some replacement text. the list of identifiers seperated by commas aperas between parentheses following the macro_name(FMAC). each identifier can apear one or more times in the substitution string
using:
example:
output:
example: