Ask Question
14 December, 08:48

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; }

Answers (1)
  1. 14 December, 09:03
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers
Sign In
Ask Question