Question discussion

What is the output of the following C++ program?#include using namespace std; template void f(T a) { static int c = 0; c++; cout

1363Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C++ program?

#include <iostream>
using namespace std;
template <class T> void f(T a) { static int c = 0; c++; cout << c; }
int main() { f(1); f(2); f(1.5); f('a'); f(3); }
  • A12345
  • B11111
  • C12113
  • D12123
Correct answer: C

Explanation

Each template instantiation has its own static variable: f is called three times (1, 2, 3 — printed at the 1st, 2nd and 5th calls) while f and f each print 1, giving 12113.

Discussion 0

Nobody has replied yet. Start the discussion!