0

I checked both by compiler that:

The output of this is 10

    int count = 0; 
    for(int i=0; i < 10; ++i){ 
            count=++count;
    }
    cout << count; 

I don't get why the output of this(++count becomes count++) is 0

    int count = 0; 
    for(int i=0; i < 10; ++i){ 
            count=count++;
    }
    cout << count; 
Jay
  • 117
  • 2
  • 2
  • 6

1 Answers1

4

With

        count=++count;

and

        count=count++;

both programs run into undefined behaviour as you are modifying count without an intervening sequence point. Note that = operator doesn't introduce a sequence point.

Obligatory read on UB ;-)

Undefined Behavior and Sequence Points in C++

Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238