-4
#include<stdio.h>
#define max(a) ((++a)*(++a)*(++a))
main(){
int a=5;
printf("%d\n",max(a));
printf("%d",a); }

this should return 6*7*8 but it is returning 7*7*8 why ?

kasinavijay
  • 168
  • 1
  • 12
  • 2
    [Undefined behaviour](http://en.wikipedia.org/wiki/Undefined_behavior) due to lack of [sequence point](http://en.wikipedia.org/wiki/Sequence_point). – Dayal rai Dec 11 '13 at 08:36
  • This is not really a macro issue. This is an issue with the resulting code, which would be bogus if you typed it directly. – RichardPlunkett Dec 11 '13 at 08:57

1 Answers1

3

Standard says that you get undefined behavior if you modify variable multiple times between sequence points. That is what you do, so that is what you get.

Note that:

1) You have no promises about order of (++a)*(++a)*(++a) evaluation, apart from operation (multiply) being done after it's arguments calculation (++a).

2) You are explicitly not allowed to modify same variable more than once between sequence points.

nyrl
  • 769
  • 5
  • 15