CJCoding With Joseph
15per day
← Back to Question List
C++ Code Walkthrough #6

Static Variable in Function

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