New course! Every coder should learn Generative AI!
Try a free lesson0
#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?
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.