Macros
A macro is a piece of code that is replace by its value before getting compiled to machine code.
Types of macros:
Object like macros
Chained Macros
Multi-line Macros
Function-like Macros
Object like Macros
Used to replace symbolic names with its value.
Chained Macros
A macro definition that contains another macro
Multi-Line Macro
A macro definiton that spans multiple lines. A \
is used before ending a line.
Funciton-like Macros
Basic single line funcitons can be implemented using a macro. This will be faster than creating a funciton as there will be no funciton call and save time and space creating entries in the call stack.
Advantages of using a Macro
Reduces code length
use
#define PB push_back
andv.PB()
#define sort(v) sort(v.begin(), v.end())
reduces sort tosort(arr)
Avoid magic number
which are random constants that programs use that only make sense to them
Loop runs for 25 times. Why? Is it because alphabets are from 0 - 26? some other reason? Who knows.
#define alphabets 25.
better. more explicit.
Faster than varibale as no extra memory is being created. No read time, write time or access time.
Saves spaces.
Last updated