-4

The following program in c outputs 37, as an answer and I am not able to come out with its logic.In other language the answer like java or javascript the output is 36. Can someone please explain the logic for the same in the mentioned languages.

C

   #include <stdio.h>
      int main(){
      int a = 10; //assignment of variable
      printf("%d",(++a + ++a + ++a)); // increment the variable
      getchar();
      return 0;
    }

Java

public class postpreincrement {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 10;// variable a assigned value
        System.out.println(++a + ++a + ++a);// evaluation of expression
    }

}

JavaScript

var a = 10; // variable a assigned value
console.log(++a + ++a + ++a); // evaluation of expression
PDG
  • 1
  • 1

1 Answers1

1

In C language, It is undefined behaviors because it attempts to modify and read the same variable without synchronization.

msc
  • 33,420
  • 29
  • 119
  • 214