s=-2
w=-2
s+=3
print(s) gives 1 and then print(w) gives -2. but sir told between -5 to 256 same adress is assinged, then change in one should reflect in other as well… how is it happening??
Address assinged to variable for int between -5 to 256
Hello @i.m.anand_31, yes this is true.
Okay see this,
s = -2 // suppose the address is 7890
// now we want to assign w = -2 and our code will search if there is any value with -2
w = -2 // as -2 was already there, its address will also be equal to 7890
// now we want to do s += 3 which means s = s + 3 and the overall value will be 1 as -2 + 3 = 1
// so 1 is not present there in the address space, so s will get the value of 1 and the new address will be assigned to this let's say 1234.
s += 3 // address is 1234
// now print(s) will give 1 and its address is 1234
print(s)
// now print(w) will give -2 and its address is 7890
print(w)
I hope it will be clear to you. In case there is some confusion or doubt you can ask it to me, I will surely help you out.
And if it is clear to you then pls mark it as resolve.