2421 Shares

Static variable question?

Static variable question? Topic: Variable case statements
June 20, 2019 / By Osman
Question: Consider the following code : void abc() { static int x=0; x++; cout<
Best Answer

Best Answers: Static variable question?

Leonard Leonard | 6 days ago
static variables are created once and memory remains allocated as long as the program executes. static variables either be Global Variables or Local Variables and the only difference between them is in their Scopes: - Scope of a Global Variable is program scope. - Scope of a Local Variable is block scope. In your case: x as static variable declared and initialized to zero once at the first call of function abc(). and the deceleration statement static int x=0; is skipped by the compiler in subsequent calls.
👍 280 | 👎 6
Did you like the answer? Static variable question? Share with your friends

We found more questions related to the topic: Variable case statements


Leonard Originally Answered: Static variable question?
static variables are created once and memory remains allocated as long as the program executes. static variables either be Global Variables or Local Variables and the only difference between them is in their Scopes: - Scope of a Global Variable is program scope. - Scope of a Local Variable is block scope. In your case: x as static variable declared and initialized to zero once at the first call of function abc(). and the deceleration statement static int x=0; is skipped by the compiler in subsequent calls.
Leonard Originally Answered: Static variable question?
Yes, its because the variable is declared as a static. This causes the the variable to be maintained even though the function begins and ends. If you did not declare the variable as a static, than when the function completed the variable would be destroyed. Subsequent calls to the function would cause the recreation of a variable, which in this case would be set to 0, so your output would be as predicted.

Jalon Jalon
Yes, its because the variable is declared as a static. This causes the the variable to be maintained even though the function begins and ends. If you did not declare the variable as a static, than when the function completed the variable would be destroyed. Subsequent calls to the function would cause the recreation of a variable, which in this case would be set to 0, so your output would be as predicted.
👍 120 | 👎 -3

Forest Forest
no the value of x is maintained by the function throughout the cycle of the loop almost like a for each loop. u must write the variable within the loop at the end to set it back to 0 each time
👍 115 | 👎 -12

Forest Originally Answered: Whats the independent variable and dependent.10 POINTS?
Control variable- thing that stays the same to keep the test fair. *type of algae *amount of water i.n containers. *the amount of days they left the containers near the window. *(initial cloudiness of water??). Independent variable- the thing that is changed in an experiment. *amount of plant fertilizer. Dependent variable- what is measured after the experiment. *The cloudiness of the water everyday. I won't help you with the problem or the hypothesis... it's your science homework(:

If you have your own answer to the question variable case statements, then you can write your own version, using the form below for an extended answer.