+ 4

postfix

int a = 10; int b = a++ + a++; //out put mebe 20; //but its showing me 21 why

4th Jun 2018, 9:58 AM
youk
3 Respuestas
+ 2
Post version prefix work in this way: they create a copy of your value, apply operation on original and return the copy value.. Then: int b= a++ + a++; first a++ create an "a" copy, increment original "a" to 1 (a=11) and return the copy "a" value (10). Note that from here "a" contain 11 Now our expression is evaluted like: int b= 10 + a++; In second a++ you apply same concept but remember that now "a" is 11 then all its evaluted like: int b= 10 + 11; then b==21 and a==12
4th Jun 2018, 10:20 AM
KrOW
KrOW - avatar
+ 1
thankx now i understand thankx for your help
4th Jun 2018, 10:32 AM
youk
0
becouse it's add to a one after it use it so int b = 10 + 11, because the second a is incremented by first a++.
4th Jun 2018, 10:10 AM
Krzysztof303
Krzysztof303 - avatar