-2

I have here an equation i can't understand how c++ process this. Can someone explain this operation?

code:

#include <stdio.h>

    main(){
        int a[10] = {0,1,2,3,4,5,6,7,8,9};
        int i = 0;
        int num = a[i+++a[++i]]+a[++i+i++];
        printf("\nnum1: %d i: %d,num,i);
    }

why is the answer num = 9 while index i is just equal to 4;

  • 1
    Oh my, you have both [maximal munch](http://stackoverflow.com/q/5341202/1708801) and [undefined behavior](http://stackoverflow.com/q/949433/1708801) issues here. There is also no C++ here. – Shafik Yaghmour Mar 11 '15 at 03:45
  • Drop that crappy line of code in favor of something a human can read and make sense of without spending an hour on how operator precedence, sequence points, and undefined behavior come into play in evaluating that line. – R Sahu Mar 11 '15 at 03:46
  • @ShafikYaghmour I agree with your other points, but why it is not C++, is it not valid , C and C++? – Pranit Kothari Mar 11 '15 at 03:49
  • It is valid C++. `printf` is valid C++. It is not stylistically good C++ and it is using a C++ method that is discouraged by nearly everybody, but `printf` is part of the C++ standard and so saying that it is "not C++" is just plain wrong. – Gort the Robot Mar 11 '15 at 03:51
  • 2
    @StevenBurnap Implicit int, however, has never been C++. – T.C. Mar 11 '15 at 03:53
  • Ok, you're right, I missed that. – Gort the Robot Mar 11 '15 at 03:55
  • the C compiler always takes the longest token which determines which '+'s go with which variable and C/C++ evaluate expressions per the rules listed in: – user3629249 Mar 11 '15 at 03:59
  • the C++ headers do not have a '.h' extension, so this is strictly C. – user3629249 Mar 11 '15 at 04:01
  • main() always returns an int, not a 'unknown' or 'void' – user3629249 Mar 11 '15 at 04:04
  • stdio.h is deprecated, but standard. See http://stackoverflow.com/questions/7596406/stdio-h-not-standard-in-c Also, leaving out the return statement in main is legal in standard C++. (But as mentioned before, it must be declared `int`.) – Gort the Robot Mar 11 '15 at 04:13

1 Answers1

2

Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.

Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.

Gort the Robot
  • 2,329
  • 16
  • 21
  • I need to know what happen. why does c++ successfully compile and execute this code if it is undefined. it is given to us by our trainor to trace what is happenning in this code. so what does really happen? – Josua Talatala Mar 11 '15 at 03:54
  • Both C and C++ successfully compile lots of undefined things. "Undefined" does not mean "won't compile". It means "what happens if you do this is undefined". If someone purporting to be teaching either C or C++ gave you this as something worth attempting to evaluate, then I'd have grave doubts as to their quality as a teacher. – Gort the Robot Mar 11 '15 at 03:57
  • (And I cannot tell you what will really happen because it entirely depends on what compiler you are using, in a way that is certainly not documented by the compiler writers.) – Gort the Robot Mar 11 '15 at 03:58