why does the output is 7 instead of 16 ????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why does the output is 7 instead of 16 ?????

#include <stdio.h> #define sqr(x) x*x int main() { printf("%d", sqr(3+1)); return 0; }

19th Apr 2021, 3:31 PM
RAJESH
6 Answers
+ 6
#define only replaces the variable name with the value. So, sqr(3+1) = 3+1*3+1 = 3+3+1 = 7 To get 16 as output, you need to use parentheses. Like this: #define sqr(x) (x)*(x)
19th Apr 2021, 3:50 PM
Genuine Stalwart
Genuine Stalwart - avatar
+ 6
To get output 16 you may use #define sqr(x) (x)*(x) Then output will be (3+1)*(3+1) (4)*(4) 16
19th Apr 2021, 4:01 PM
TOLUENE
TOLUENE - avatar
+ 3
Rajesh Dutta oo, sorry. My bad 😅 I wrote 9 instead of 7 by mistake 😁
20th Apr 2021, 3:53 AM
Genuine Stalwart
Genuine Stalwart - avatar
+ 2
Martin Taylor Genuine Stalwart Sayed🇧🇩🇧🇩 This was in solo learn challenge, solo learn challenged me and the question was as it is like this, the correct answer says 7
20th Apr 2021, 3:26 AM
RAJESH
+ 1
Genuine Stalwart thanks i finally understood that problem 👏👏
20th Apr 2021, 6:28 AM
RAJESH
+ 1
Martin Taylor sololearn actually challenged me and this was the question as it is written so, i answered 16 but it showed correct answer is 7👍
20th Apr 2021, 7:17 PM
RAJESH