+ 2

How incrementing works?

How the answer become -3? int a = -5; int k = (a++, ++a); printf("%d" , k);

16th Nov 2021, 11:28 AM
Morteza Hajji
4 Antworten
+ 2
Tnx for the answer, but i have confused with post incremet, it will apply after semicolon. Shouldn't the answer be -4?
16th Nov 2021, 5:27 PM
Morteza Hajji
+ 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
16th Nov 2021, 6:21 PM
Jayakrishna 🇮🇳
+ 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..
16th Nov 2021, 12:19 PM
Jayakrishna 🇮🇳
+ 1
tnx for you comprehensive response
16th Nov 2021, 9:19 PM
Morteza Hajji