0

I am getting 30 & 10 as output. I don't understand why, please explain why 30?

#include<stdio.h>

void main() {
  int arr[]={10,20,30,40,50};
  int *ptr=arr;
  //printf("%d",*ptr);
  printf("%d %d",*++ptr,*ptr++);
}
JJJ
  • 32,246
  • 20
  • 88
  • 102
  • 3
    It is [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). The compiler is allowed to evaluate the arguments of a function call in what order it prefers. Using different compilers with different compiling options (different optimization levels) you can get any combination of `10`, `20` and `30` in the output. – axiac Nov 28 '17 at 10:46
  • @axiac the evaluation order by itself is just unspecified behavior, it's still defined to evaluate all arguments in some order. I'm not quite sure whether the ++ operator is really a good candidate for undefined behavior, it might as well be unspecified behavior with an undefined value - many answers tend to mix everything in the UB pot instead of making a difference in this aspect. – grek40 Nov 28 '17 at 11:49

0 Answers0