FAHEEM_KAHN's blog

By FAHEEM_KAHN, history, 3 hours ago, In English
#include <iostream>
using namespace std;
int main() {
    int a=1;
    int b;
    b = ++a * ++a;
    cout<<"a: "<<a<<", b: "<<b; cout<<endl;

    a=1; b = a++ * a++;
    cout<<"a: "<<a<<", b: "<<b; cout<<endl;

    a=1; b = ++a * a++;
    cout<<"a: "<<a<<", b: "<<b; cout<<endl;

    a=1; b = a++ * ++a;
    cout<<"a: "<<a<<", b: "<<b; cout<<endl;
}

OUTPUT:
// a: 3, b: 9
// a: 3, b: 2
// a: 3, b: 6
// a: 3, b: 3

Can anyone explain why, this is the output?

Tags cpp
  • Vote: I like it
  • -5
  • Vote: I do not like it

»
2 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Modifying a variable more than once within the same expression (without sequence points or inter-variable sequencing) results in undefined behavior

  • »
    »
    42 minutes ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    tnks