#define | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

#define

#include <iostream> using namespace std; #define x 5+2 int main() { int i; i = x*x*x; cout << i; return 0; } // output is 27 How does x become 3? How does #define x 5+2 come into play?

24th Jul 2020, 2:56 AM
Solus
Solus - avatar
1 Answer
+ 2
The #define is part of the preprocessor language for C and C++. When they're used in code, the compiler replaces the #define statement with whatever we want. x is replaced by 5+2 so we have 5+2*5+2*5+2, which the compiler translates to 5+10+10+2, or 27.
24th Jul 2020, 3:11 AM
Solus
Solus - avatar