InterviewSolution
Saved Bookmarks
| 1. |
In C/C++, define macros. Give an example to illustrate your point. |
|
Answer» In C/C++, macros are preprocessor directives that are replaced at COMPILE time. As a result, a macro in a program is a named section of code. The compiler substitutes this name with the actual piece of code when it recognises it. The DRAWBACK of macros is that they are code changes rather than function calls. They also offer the ADVANTAGE of saving time when substituting IDENTICAL values. All instances of the terms TEXT, EVEN, and SUMMATION in the sample code snippet below will be replaced with whatever is in their body. #include <bits/stdc++.h>// Macros can be defined as shown below#define ADD (2 + 1)#define SUB (2 - 1)#define MUL (2 * 1)#define DIV (2 / 1)// Main function of the C++ Programint main(){ cout << "The value of the sum of the given numbers is: " << ADD << "\n"; cout << "The value of the difference of the given numbers is: " << SUB << "\n"; cout << "The value of the product of the given numbers is: " << MUL << "\n"; cout << "The value of the division of the given numbers is: " << DIV << "\n"; return 0;} |
|