1.

What exactly are macros? What are the benefits and drawbacks?

Answer»

Macros are pre-processor constants that are replaced at compile time. Thus, a macro is a section of CODE in a program that has been given a name. The compiler substitutes this name with the ACTUAL PIECE of code whenever it encounters it.

The downside of macros is that they are not FUNCTION calls; they simply change the code. Similarly, they have the advantage of saving time when substituting the same values.

#include <stdio.h>// defining macros#define TEXT "Hello"#define EVEN 2#define SUMMATION (8 + 2)int main(){ printf("String: %s\n", TEXT); printf("First Even Number: %d\n", EVEN); printf("Summation: 8+2=%d\n", SUMMATION); return 0;}

Any instance of the terms TEXT, EVEN and SUMMATION in the sample above will be substituted by whatever is in their body.



Discussion

No Comment Found