+ 2
How incrementing works?
How the answer become -3? int a = -5; int k = (a++, ++a); printf("%d" , k);
4 Answers
+ 2
Tnx for the answer, but i have confused with post incremet, it will apply after semicolon. Shouldn't the answer be -4?
+ 2
Yes. Post increment operator is applied increment after using the value. But there a++, ++a are two separate instructions , not a compound ones.
Post not means after semicolon, it means after the current instruction.
So there a++, ++a are camma separated independent,different instructions so happens like a, a++, ++a , k=a
even if it is
k= a++ + ++a ; //then also it works like assume a=1, then
k = 1 + 3 , a, a++, now updated a++ value=2, is used in ++a so ++a is replaced by 3, but note a++ replaced by 1 only, but after it a=2, which is not used in a++, but ++a value used in ++a..
k = 4
+ 1
Series of instructions executed are :
a=-5;
a++; //a=a+1=-4
++a; //a=a+1=-3
Now this last value of a is assigned to k.
if you need, search bar will provide lot of answers about post/pre increment operators executions..
hope it helps..
+ 1
tnx for you comprehensive response