0
Why this answer is 12? Can help me to explain it?
int sum (int i) {return ++i;} int main () { for(int = 0; i <=1; i++) {cout<<sum(i); }
8 Antworten
+ 7
The answer isn't twelve really, but rather the for loop first outputs 1 and then outputs 2, and they are placed next to each other because there is no return ("\n" or endl) in the cout.
+ 5
1st cycle: the return value is 1 (first i value is incremented then returned)
2nd cycle: like the first cycle 1 incremented to 2 and then returned to caller.
+ 5
Yeah dear, you are right.
Delete that.
+ 3
If you were to change the for loop so that the conditional were i <= 2 instead, then the output would be 123. If you added << endl to the end of the cout so that it reads:
cout << sum(i) << endl;
the output will be
1
2
3
outputing the numbers all on different lines instead of next to each other on the same line.
+ 2
@Babak no that will change the output entirely, because you are now incrementing the i in the for loop twice.
+ 1
lul
0
so if the code never use return on the main function, it will has 12 for the answer?
0
thanks for the answer..finally i got it