Ask Question
25 June, 16:01

What will the following code display? #include using namespace std; void doSomething (int); int main () { int x = 2; cout << x << " "; doSomething (x); doSomething (x); cout << x << endl; return 0; } void doSomething (int num) { static int x = 0; cout << x << " "; x=num; }

+3
Answers (1)
  1. 25 June, 18:05
    0
    The output will be: 2 0 2 2

    Explanation:

    First of all, you need to complete the statement "#include". It is "#include ".

    Static variables in a Function: When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once and the value of variable in the previous call gets carried through the next function call.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “What will the following code display? #include using namespace std; void doSomething (int); int main () { int x = 2; cout ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers