Ask Question
29 May, 00:34

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. 29 May, 02:58
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers