Why macros are used in C?


Macro in C

In C, the macro can be used to define any constant value or any variable with its value that will be replaced by this macro name in the entire program. The macro contains the set of code that will be called when the macro name is used in the program. Macros are generally faster than functions because they do not have the overhead of the actual function.

Inline Functions

When called, a function is expanded in line. The complete content of the inline function is inserted or replaced at the point of inline function call. The compiler C++ performs this substitution during compilation. Inline functions are those whose definitions are small and are replaced where its function call occurs.

Macro in C:

In both object-like and function-like macros, the double-number-sign or token-pasting operator (##), sometimes referred to as the merging or combining operator, is used to scan for potential replacements.

In C, where are macros stored?
Instead of being stored in memory in the final program, the macro code is repeated each time it occurs. The actual compiler does not even have it; it was replaced by the preprocessor before reaching that point.

Lascia un commento