Increment n Decrement(URGENT HELP) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Increment n Decrement(URGENT HELP)

Please help me to understand the output of this code. I am really confused. I know the output but kindly explain me its working in memory?. That single operator with operand (-a), (b=-b), (+c), (d= -c) is different for me. int a,b,c,d; a=b=c=d=0; a++; -a; b= a++; b= -a; c += a - b; c = ++c; +c; d = -c; cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl;

21st Oct 2018, 3:13 PM
Moeez Shahid
Moeez Shahid - avatar
2 Answers
+ 1
int a,b,c,d; a=b=c=d=0; a++; //a still 0 but will become 1 after this statement -a; //a=1 now, so this one is -1 b= a++; //b=1, but a will increase by 1 after this b= -a; //a = 2 now, so b=-2 c += a - b; // this equal to c=c+a-b or c= 0+2-(-2) = 4 c = ++c; // c increase by one first (5) then assign to c +c; // positive c 5 nothibg change d = -c; // d=-5 cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl;
21st Oct 2018, 4:43 PM
Taste
Taste - avatar
0
thanks alot, it really helped me.❤️
21st Oct 2018, 5:31 PM
Moeez Shahid
Moeez Shahid - avatar