+ 1

Why is output of this code is 65 and why ain't it is 225?

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

9th May 2020, 12:30 PM
Abhinav Gourav Simhadri
Abhinav Gourav Simhadri - avatar
3 Antworten
+ 2
Because #define is just an expression, when the compiler encounters "x" it doesn't calculate the operation, it simply replaces x with 10+5. So you will have: int a = 10+5*10+5; // a = 10+50+5 = 65 If you want the result to be 225 you should write the code like this: int a = (x)*(x);
9th May 2020, 12:34 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
+ 2
Simply it will be calculate --- 10+5*10+5 so answer is 65
11th May 2020, 10:29 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Rising Abhinav , because the calculation is the following => 10+5*10+5 => 65. If you place brackets and change the line define x (10 + 5), I think you'll get the desired result.
9th May 2020, 12:33 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar