-2

I am trying the following code:

#include <stdio.h>

int main()
{
    int c =0;
    c -= --c - c++;
    printf("%d \n",c);
    return 0;
}

When I compiled and run it using a online c complier (https://www.tutorialspoint.com/compile_c_online.php) the result is -1. But I expected it to be 0. So, I try it on my local Dev C++ (Windows) and the result is 0.

  1. Should the result be 0 ?
  2. If so, why 2 gcc compilers (ok they are in different plataform) gives me 2 different results ?

I ve been looking for some kind of automatic flag otimization which could produce a different result but I had no success.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
ricksant
  • 117
  • 9

1 Answers1

4

THIS IS UNDEFINED BEHAVIOR (3 modifications without sequence points inbetween to the same variable)

Paul Stelian
  • 1,381
  • 9
  • 27