+ 5

Why the output is 65 instead of 225 ?

#include <stdio.h> #define x 10+5 int main() { int a = x*x; printf("%d", a); return 0; }

16th Feb 2022, 3:53 AM
SIMRAN
SIMRAN - avatar
4 Respostas
+ 5
SIMRAN #define x 10 + 5 //here x = 10 + 5 So x * x = 10 + 5 * 10 + 5 = 10 + 50 + 5 = 65 ------- #define x (10 + 5) //here x = (10 + 5) So x * x = (10 + 5) * (10 + 5) = 225 ------- #define x (10) + (5) //here x = (10) + (5) So x * x = (10) + (5) * (10) + (5) = 10 + 50 + 5 = 65
16th Feb 2022, 4:31 AM
A͢J
A͢J - avatar
+ 5
10+5*10+5 Edit: you could instead #define x (10+5) EDIT2: I think that define works before compilation, it does a (search replace) with the defined term definition. eg if you 👉#define x 5👈 it may just replace xray to 5ray. Let's test that, I will report back what I find! EDIT3: I don't think macro #define replaces letters when they are part of a longer word. hmmm.... https://code.sololearn.com/cl05IIc37s4E/?ref=app
16th Feb 2022, 4:12 AM
HungryTradie
HungryTradie - avatar
+ 4
I got it , Thank you Ipang ,A͢J & HungryTradie 🤗❤️
16th Feb 2022, 11:01 AM
SIMRAN
SIMRAN - avatar
+ 3
x * x will be replaced with 10 + 5 * 10 + 5 which will be processed as 10 + ( 5 * 10 ) + 5 due to operator precedence (* priority is higher than +) Hope it makes sense ...
16th Feb 2022, 4:14 AM
Ipang