-5

When I compile the following code

#include<stdio.h>
#define CUBE(x) (x*x*x)

int main()
{
    int a, b=3;
    a = CUBE(b++);
    printf("%d, %d\n", a, b);
    return 0;
}

It gives 27 , 6

But shouldn't the expression a=b++*b++*b++; be calculated as a=3*4*5 and should give 60?

Naveen
  • 7,944
  • 12
  • 78
  • 165

1 Answers1

4

Your expression causes undefined behaviour, so you could get any answer. Trying to modify the same value more than twice between sequence points is bad news.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469