Why the Output is 16? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why the Output is 16?

I thought the output will be 1, But i've got 16. #define sqr(x) x*x int x = 16/sqr(4); printf("%d", x); Why...?

19th Jan 2021, 7:19 AM
Asrar
Asrar - avatar
2 Answers
+ 3
#define macros aren't functions even if they look so. They actually just kind of copy and paste code. In your case the macro is preprocessed to: int x = 16/4*4; That's 16. If you want to avoid this you have to use parenthesis around your macro like this: #define sqrt(x) (x*x)
19th Jan 2021, 7:41 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 3
Mirielle and Aaron Eberhardt Thank you guys
19th Jan 2021, 7:56 AM
Asrar
Asrar - avatar