-3

When i run this code the var "tarkiz" remains 1. Could anyone please explains to me why this happens? isn't it supposed to perform the assignment first (tarkiz = tarkiz) and then increment the value to be 2 instead of 1?

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int tarkiz = 1;
    tarkiz = tarkiz++;
    cout<<tarkiz<<endl;
    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Dreadly
  • 45
  • 3

1 Answers1

2

tarkiz = tarkiz++; is undefined behavior. To fix it write tarkiz++; instead.

See this explanation for details.

Community
  • 1
  • 1
nwp
  • 9,623
  • 5
  • 38
  • 68