#include <iostream> using namespace std; int main() { int num=10; int *ptr=NULL; ptr=# num=(*ptr)++; //it should increase to 11 num=(*ptr)++; //it should increase to 12 but im getting 10 //if i dont initialize num and just use (*ptr)++ it gives me 11 cout<<num<<endl; return 0; } I want to know why is this happening and why am I getting 10 as output.
num=(*ptr)++;exhibits undefined behaviour. Compilers will likely not spot it but, replacing withnum = num++will generate warning : multiple unsequenced modifications to 'num' [-Wunsequenced] with clang-cl (using the C++14 Standard).