+ 6

What does this mean: #define x 5+2? And how would C++ colpile it?

I fond this: #define x 5+2 int main(){ int i= x*x*x; cout << i; return 0; } // the out put is 27 !!! i don't understand how !!! Can any one explane, please!

7th Mar 2018, 1:49 PM
Ali Kafi
Ali Kafi - avatar
4 Respostas
+ 16
@ KrOW @Ace, thanks for the good explanation 👍😉
7th Mar 2018, 2:04 PM
tooselfish
tooselfish - avatar
+ 6
#define run in preprocessor context and when it run before compilation... it essentially replace the content of defined identifier with corrispondent value in your case: #define x 5+2 int i= x*x*x; here x will be substituited to 5+2 as: int i= 5+2*5+2*5+2; that is 5+10+10+2 = 27
7th Mar 2018, 1:59 PM
KrOW
KrOW - avatar
+ 3
@KrOW @Ace ; Thanks a lot for answering me. So, if i understand well; in this example: #define x 10+10 int i=x/10; cout<<i; It will be compiled like int i=10+10/10; and the result will be 11
7th Mar 2018, 2:13 PM
Ali Kafi
Ali Kafi - avatar
+ 2
👍
7th Mar 2018, 2:14 PM
KrOW
KrOW - avatar