0
#define c(x) (x*x*x)

void main()
{
    unsigned int i = 5;     
    printf("the value of a is %d\n\r", ((++i) * (++i) * (++i)));
}

the answer is 392 which is the multiples of 7 * 7 * 8; can any one explain the cause? and also if the variable i is declared globally i get 336

#define c(x) (x*x*x)
unsigned int i = 5; 

void main()
{
    printf("the value of a is %d\n\r", ((++i) * (++i) * (++i)));
}

which is the multiples of 8 * 7 * 6; this is because the increment precedence is from right to left for printf and left to right for ++ postfix operator.

there is also another compliance here which is if you declare the variable as volatile you again get 336 even when the variable is declared locally.

#define c(x) (x*x*x)

void main()
{
    volatile unsigned int i = 5;        
    printf("the value of a is %d\n\r", ((++i) * (++i) * (++i)));
}

is this something to do with the local variable which is stored in the stack? are the variables in stack stored in a local reg and the compilation happens directly from the register and not from the memory? please explain this its bugging me from a very long time?

  • 2
    Could you remove that confusing `#define`? It doesn't seem to have anything to do with the question. Anyway, you're seeing the effects of *undefined behaviour*. – juanchopanza Jun 30 '15 at 09:36
  • #define is actually kept for the replacing the expression as printf("the value of a is %d\n\r", c(++i)); from printf("the value of a is %d\n\r", ((++i) * (++i) * (++i))); k anyways thankyou for your valuable time folks, i appreciate it. these answers showered the light of undefined behaviour on me thank you again? – augustine nishil Jun 30 '15 at 10:49
  • Somehow I like the "presidence" here...:P (No offence, please) – Sourav Ghosh Jun 30 '15 at 11:48
  • 'bugging me from a very long time?' - what since your prof/TA set it? – Martin James Jun 30 '15 at 15:08
  • no its an interview question i answered it partially right – augustine nishil Jul 02 '15 at 07:07

0 Answers0