-2

Hi need to under stand execution steps for below program.

#include<stdio.h>
#define SQUARE(x) (x)*(x)
void main()
{
 int i = 5;
 printf("%d\n", SQUARE(++i));
}

As i have understanding like macro expanded like (++i)*(++i) and as result (7)*(6) = 42 but the output is 49 which i don't understand why?

user2520119
  • 173
  • 11
  • 'need to under stand execution steps for below program' why? If it's too difficult to easily understand, (which it is), it's not fit for purpose. Get rid of the pre-increment and add an increment as a separate line above the macro 'call'. – ThingyWotsit Jul 02 '17 at 07:52

1 Answers1

1

That's precisely the problem with macros, if you don't pay attention, you end up with undefined behaviour. See Why are these constructs (using ++) undefined behavior?

(++i)*(++i) is undefined behaviour, the result can be anything including exploding your computer.

Pablo
  • 13,271
  • 4
  • 39
  • 59