+ 1
Can anyone help me understand these output questions? (C++)
i'm good at inputs, but very bad at outputs. I've taken these questions from the challenge game because even though it shows the right answer, it doesn't explain. 1. p = 8 cout << p++; cout << ++p; ANSWER : 810? 2. int x = 10; void calculate (int&a) { a = a + 10; cout << a; } int main () { int x = 20; calculate (x); cout << x+20; ANSWER : 3050 3. int a = 2; int b = 1 ; cout << (++a / b ++) * 5; ANSWER : 15
3 Antworten
+ 2
1. ++x and x++ have a small difference.
++x:
First increments x by 1, then returns the value of x.
x++:
First returns the value of x, then increments x by 1.
3:
a = 2, b = 1
(++a / b++) * 5 can be thought as ((a+1) / b) * 5, a += 1, b += 1
So:
((2+1) / 1) * 5 = 3 * 5 = 15
+ 3
Many people get confused with prefix and postfix increment (1 and 3 questions). Please refer to these existing questions
https://www.sololearn.com/discuss/1492044/?ref=app
https://www.sololearn.com/discuss/948304/?ref=app
https://www.sololearn.com/discuss/1562989/?ref=app
+ 2
https://code.sololearn.com/c5dx79XNNrpD/?ref=app
Check this example also when you understand prefix and postfix