CJCoding With Joseph
15free / daySign inCreate account
Back to Code Walkthrough

Static Variable in Function

C++ code walkthrough · Question 6
mediumFunctions

Code

Read carefully. What does this print?
1#include <iostream>
2using namespace std;
3 
4int counter() {
5 static int count = 0;
6 count++;
7 return count;
8}
9 
10int main() {
11 cout << counter() << endl;
12 cout << counter() << endl;
13 cout << counter() << endl;
14 return 0;
15}

Your answer