How does a # define function works in C++, explain the output of the code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How does a # define function works in C++, explain the output of the code

https://code.sololearn.com/cEWyr0k26Ynq/?ref=app

27th Aug 2017, 5:20 PM
Sumit Singh
Sumit Singh - avatar
8 Answers
+ 2
Naitomea, nice work on figuring that out. I think you are probably correct. Exactly, Sumit...why is it acting like a loop almost, where it changes the value of the incremented x each time it multiplies? And why is the behavior of the prefix increment so different than the postfix? These are answers I definitely don't have, as I have little to no experience with using #define in the first place.
28th Aug 2017, 3:30 PM
Zeke Williams
Zeke Williams - avatar
+ 1
I messed with your code for a bit to try to figure out what was happening, and I'm still not sure what is happening at all. What I DO know is that it has to do with the prefix increment. If you call Sqr () with just a, it works like I think it should. Mathematically, ++a when a is 2 yields 80 (2 * 5 * 8). When a is 3 it yields 150, so I don't know what is happening. If be interested to see someone else's input.
28th Aug 2017, 3:38 AM
Zeke Williams
Zeke Williams - avatar
+ 1
As if Zeke did, I did some experiments with your code, and came up with this explanation for the output: If you just call a, it evaluates a*a*a -> 2*2*2, which is 8. Okay, makes sense. Next with postfix: a++ * a++ * a++ -> 2 * (2+1) * (3+1) -> 2*3*4 -> 24. Postfix means increment after the evaluation, so it takes two as the first x, then increments it to three, calculates this and then increments to four. Makes sense too I guess. Now with Prefix: Sqr (++a) with a=2 is 80. Mathematically you can say ++a * ++a * ++a or 4*4*5=80. I think this is because Prefix increments before evaluation, so it first increments a to 3, but it didn't calculate yet, so it increments a again to 4. Afterwards it calculates the first block, which is now 4*4. Then increment again to 5, calculates 16*5 which is 80. Another example is a=3 -> 5*5*6 -> 150. Same goes for decrement. Im not sure if this is totally correct, but to me it makes somewhat sense.
28th Aug 2017, 8:35 AM
Shadow
Shadow - avatar
+ 1
thanks to all of u to give it a thought. I also did this for different inputs , I know #define is a macro. and my confusion is same as all of u. If I change prefix increment to postfix for a =2, ie, a++ than func Sqr() calculates as 2*3*4 to give 24. which is it incrementโ€‹ after each assignment and in prefix " Naitomea" explained it perfectly. I also have another question that why is it assigning different value to x for each x( x* x* x) for both pre increment and post increment. if Sqr() implemented as normal function it takes only one value for x
28th Aug 2017, 12:49 PM
Sumit Singh
Sumit Singh - avatar
+ 1
I found an explanation! It's at the top of this page: http://www.studytonight.com/cpp/inline-functions.php
4th Sep 2017, 5:44 PM
Zeke Williams
Zeke Williams - avatar
+ 1
thanks Zeke Williams, for the link. it do explains the spacing and expression execution problem in macros, appreciated ๐Ÿ‘
4th Sep 2017, 5:56 PM
Sumit Singh
Sumit Singh - avatar
0
#define is a preprocessor directive that is used as a macro to replace the contents (ur case sqrt(x)) during compilation... wherever sqrt(x) is encountered it is replaced with x*x*x in your program while compilation... and you've have used increment operations and it's output is compliler dependent
27th Aug 2017, 6:26 PM
Sai Krish
Sai Krish - avatar
0
so if x =2, output should be 3*3*3,ie , 27
27th Aug 2017, 6:42 PM
Sumit Singh
Sumit Singh - avatar