+ 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; }
28 ответов
+ 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
+ 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
+ 5
Ahmed El Mashad
square (x) x * x
square (4 + 1)
= 4 + 1 * 4 + 1
= 4 + 4 + 1
= 9
+ 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.
+ 2
taking time to explaining with words, but not deserve the best answer mark :D
+ 2
🅘🅜 🅰🅹 !!! I also matter for the sake of Q&A section: if votes and best answers were better used, that would be great ;P
+ 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.
+ 2
Calvin Thomas in (much more) other words that's what I've explained in my (concise) answer ^^
+ 1
visph thank you soo much for answering my question and explaining :)
+ 1
no problem Ahmed El Mashad ;)
+ 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?
0
🅘🅜 🅰🅹 !!! thank u soooo much. you are sooo kind. Brilliantly explained
0
Ahmed El Mashad mark 🅘🅜 🅰🅹 !!! answer as best if you think it deserve more than mine ;)
0
It uses BODMAS....Remember school 🙂
0
25
0
Prakamya Khare this thread is about C, not python...
0
visp I know buddy I put it because for the explanation :).
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
0
Well
Using (a+b)^2 yields 25 :'V