1.

Define Macros in C/C++. Explain with an example.

Answer»

Macros in C/C++ are constants in the preprocessor that are replaced at compile time. As a result, a macro is a named block of code within a programme. When the COMPILER detects this NAME, it replaces it with the actual piece of code. The disadvantage of macros is that they are not function calls, but RATHER code CHANGES. Similarly, when substituting the identical values, they have the advantage of saving time.

In the sample code snippet given below, all instances of the phrases TEXT, EVEN, and SUMMATION will be replaced with whatever is in their body.

#include <bits/stdc++.h>// Macros are being defined below#define HELLOWORLD "HELLO WORLD!"#define EVENNUMBER 4#define ODDNUMBER 3#define ADD (4 + 3)// Main function of the C++ Programint main(){ cout << "String: " << HELLOWORLD << "\n"; cout << "Even Number is: << EVENNUMBER << "\n"; cout << "ODD Number is: << ODDNUMBER << "\n"; cout << "The sum of the given even and odd numbers is: " << ADD << "\n"; return 0;}


Discussion

No Comment Found