What is output of this code? (C language) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is output of this code? (C language)

Thank you in advance for taking time to answer my question. What is output of this code? and please why ? #include <stdio.h> #define square(x) x * x int main() { printf("%d", square(4+1)); return 0; }

22nd Mar 2021, 7:58 PM
Hodeni
28 Answers
+ 7
run it in a code playground to get the output ;) and why is because there's a #define preprocessor insrtruction wich replace the square() call with the result expression... so printf() call at compile time result to: printf("%d", 4+1 * 4+1); now, you could see that operators precedence will make a different result that the one expected, as: (4+1) * (4+1) != 4+(1 * 4)+1
22nd Mar 2021, 8:18 PM
visph
visph - avatar
+ 9
Ahmed El Mashad If there is ( ) arround x in definition like square (x) (x) * (x) Then output would be different. square (x) (x) * (x) square (4 + 1) = (4 + 1) * (4 + 1) = 16 + 4 + 4 + 1 = 25
22nd Mar 2021, 8:22 PM
A͢J
A͢J - avatar
+ 5
Ahmed El Mashad square (x) x * x square (4 + 1) = 4 + 1 * 4 + 1 = 4 + 4 + 1 = 9
22nd Mar 2021, 8:17 PM
A͢J
A͢J - avatar
+ 4
visph No problem it is upto him. We cannot say anything. It's his choice to mark best answer. Doesn't matter for me.
22nd Mar 2021, 8:31 PM
A͢J
A͢J - avatar
+ 2
taking time to explaining with words, but not deserve the best answer mark :D
22nd Mar 2021, 8:27 PM
visph
visph - avatar
+ 2
🅘🅜 🅰🅹 !!! I also matter for the sake of Q&A section: if votes and best answers were better used, that would be great ;P
22nd Mar 2021, 8:35 PM
visph
visph - avatar
+ 2
I am really sorry i am new in the app visph 🅘🅜 🅰🅹 !!! . I didn’t know that the check mark mean anything I thought it’s like a thumbs up. I love both of your answers. thank you both so much for taking time to answer me. I am really sorry :)
22nd Mar 2021, 8:37 PM
Hodeni
+ 2
Sorry for my previous comment, which was vague enough to be considered useless. Ahmed El Mashad Here, 'x' isn't being substituted by the input you had fed into the function (I'm not sure whether I'm using the terms correctly) square; it's just being replaced by the input. As I understand, here, 'x' can't be considered a variable, as it doesn't hold any value; the compiler just replaces every occurrence of 'x' with '4+1', which is then simplified like this: 4 + 1 * 4 + 1, which's equal to 9. Instead, try using an extra bracket around '4+1' so that 'x' is replaced by '(4+1)', which then evaluates to (4+1) * (4+1) = 25. I hope this helps. Let me know if I've said anything incorrect. P.S. You could also use brackets around x. From what it seems, it looks like some kind of formatting, which is then simplified accordingly.
24th Mar 2021, 7:58 AM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Calvin Thomas in (much more) other words that's what I've explained in my (concise) answer ^^
24th Mar 2021, 11:17 AM
visph
visph - avatar
+ 1
visph thank you soo much for answering my question and explaining :)
22nd Mar 2021, 8:25 PM
Hodeni
+ 1
no problem Ahmed El Mashad ;)
22nd Mar 2021, 8:38 PM
visph
visph - avatar
+ 1
Calvin Thomas wich is difference between 'substitute' and 'replace' for you? by the way, where have you seen someone talking of 'substitute' in this thread?
24th Mar 2021, 4:28 AM
visph
visph - avatar
0
🅘🅜 🅰🅹 !!! thank u soooo much. you are sooo kind. Brilliantly explained
22nd Mar 2021, 8:24 PM
Hodeni
0
Ahmed El Mashad mark 🅘🅜 🅰🅹 !!! answer as best if you think it deserve more than mine ;)
22nd Mar 2021, 8:30 PM
visph
visph - avatar
0
It uses BODMAS....Remember school 🙂
23rd Mar 2021, 12:41 PM
Sanjay Kamath
Sanjay Kamath - avatar
0
25
23rd Mar 2021, 1:24 PM
Gökdeniz Dağ
Gökdeniz Dağ - avatar
0
Prakamya Khare this thread is about C, not python...
24th Mar 2021, 4:30 AM
visph
visph - avatar
0
visp I know buddy I put it because for the explanation :).
24th Mar 2021, 4:33 AM
Prakamya Khare
Prakamya Khare - avatar
0
Prakamya Khare explanation of what? 25 is the output expected by OP, but its code give it 9 ^^ by the way, posting another code language without explaining your purpose have few probabilities to help ;P
24th Mar 2021, 4:36 AM
visph
visph - avatar
0
Well Using (a+b)^2 yields 25 :'V
24th Mar 2021, 4:46 AM
Prakamya Khare
Prakamya Khare - avatar