HELP: C programming | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

HELP: C programming

Why will the following question produce 3, instead of 4? #include <stdio.h> #define square(x) (x*x) int main() { int x, y = 1; x=square(y+1); printf("%d\n",x); return 0; }

9th Apr 2020, 9:15 AM
Bruce
Bruce - avatar
2 Answers
+ 1
#define is a macro, it will just replace square(expressions) by expression*expression. so x=square(y+1)=y+1*y+1=1+1*1+1=3 if you want it to really calculate the square, use: #define square(x) (x)*(x)
9th Apr 2020, 9:24 AM
John Robotane
John Robotane - avatar
0
I'm saved, thank you John Robotane 😆
11th Apr 2020, 8:25 AM
Bruce
Bruce - avatar