+ 1

The output for this code is 11. Can anybody explain the reason

template class<T> void f(T){ int i= 0; cout<<++i; } int main() { f(2); f(4.2); }

3rd Oct 2017, 6:44 AM
Sowjanya Chunduri
6 Réponses
+ 15
the output should be 11 because you can see i is always initialised with 0,so the function f will always output 1 irrespective of the passed parameter.
3rd Oct 2017, 6:59 AM
Amit Mathew
+ 14
it should be 11,why will the output be in different lines as no \n or endl is used.
3rd Oct 2017, 7:18 AM
Amit Mathew
+ 2
cout doesn't add endline by itself, you have to explicitly specify it. cout << ++i <<endl;
3rd Oct 2017, 4:35 PM
Prince Gupta
Prince Gupta - avatar
+ 2
i will be initialized to 0 each time function will be called so thats two times 1 gets printed and as no endl or /n therefore no new line .......isnt it simple
4th Oct 2017, 7:48 AM
Saheb Kumar
Saheb Kumar - avatar
0
ohkay..so it must be 1 1 and not "11" ! I got confused with this
3rd Oct 2017, 7:14 AM
Sowjanya Chunduri
0
f() is called twice ..so I think it should be in different lines
3rd Oct 2017, 7:19 AM
Sowjanya Chunduri