What is the value of the static variable declared inside a function?

Gurpreet

A static integer variable is declared inside a function. The variable increments by 1 when the function is called. The function is called and returns 3 times. What is the value of the variable on the third function call and what is the value when it returns?

Shravan40
int foo() {
   static int x = 0;
   x++;
   return x;
}

The keyword static acts to extend the lifetime of a variable to the lifetime of the program; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How/what is the value of a variable in a nested function declared nonlocal set to?

From Dev

How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

From Dev

Python - Static Variable inside Function

From Dev

php static variable inside function

From Dev

How to change the value of a variable that is declared out of a function?

From Dev

Will a variable declared with cdef outside a function have the same type inside the function?

From Dev

how/when is a static variable in a function declared within C language?

From Dev

How to modify a local static variable without calling the function where it is declared?

From Dev

How to declare a Static Variable inside a function in Typescript?

From Dev

PHP Static variable inside a function not incrementing

From Dev

static variable scope inside and outside the function

From Dev

Javascript: While the variable is declared in global scope, it remains undefined inside the function

From Dev

Change the value of a variable inside a function

From Java

What is the Python equivalent of static variables inside a function?

From Dev

Why can a static member function only be declared static inside the class definition and not also in its own definition?

From Dev

What memory used for variable declared outside of a method or function

From Dev

What is the utility of declaring a static variable in function?

From Dev

Is the initialization of global variable the same as the initialization of static variable inside a function

From Dev

Is the initialization of global variable the same as the initialization of static variable inside a function

From Dev

Firing a function whenever static variable value changed

From Dev

Scope of variable declared inside a for loop

From Dev

Scope of variable declared inside a for loop

From Dev

Can instance variable be declared as static variable in java

From Dev

Can instance variable be declared as static variable in java

From Dev

Removing an action declared inside a function

From Dev

Changing global variable value inside function

From Dev

Variable inside lambda function is showing garbage value

From Dev

variable incremented inside function has value 1

From Dev

Changing global variable value inside function

Related Related

  1. 1

    How/what is the value of a variable in a nested function declared nonlocal set to?

  2. 2

    How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

  3. 3

    Python - Static Variable inside Function

  4. 4

    php static variable inside function

  5. 5

    How to change the value of a variable that is declared out of a function?

  6. 6

    Will a variable declared with cdef outside a function have the same type inside the function?

  7. 7

    how/when is a static variable in a function declared within C language?

  8. 8

    How to modify a local static variable without calling the function where it is declared?

  9. 9

    How to declare a Static Variable inside a function in Typescript?

  10. 10

    PHP Static variable inside a function not incrementing

  11. 11

    static variable scope inside and outside the function

  12. 12

    Javascript: While the variable is declared in global scope, it remains undefined inside the function

  13. 13

    Change the value of a variable inside a function

  14. 14

    What is the Python equivalent of static variables inside a function?

  15. 15

    Why can a static member function only be declared static inside the class definition and not also in its own definition?

  16. 16

    What memory used for variable declared outside of a method or function

  17. 17

    What is the utility of declaring a static variable in function?

  18. 18

    Is the initialization of global variable the same as the initialization of static variable inside a function

  19. 19

    Is the initialization of global variable the same as the initialization of static variable inside a function

  20. 20

    Firing a function whenever static variable value changed

  21. 21

    Scope of variable declared inside a for loop

  22. 22

    Scope of variable declared inside a for loop

  23. 23

    Can instance variable be declared as static variable in java

  24. 24

    Can instance variable be declared as static variable in java

  25. 25

    Removing an action declared inside a function

  26. 26

    Changing global variable value inside function

  27. 27

    Variable inside lambda function is showing garbage value

  28. 28

    variable incremented inside function has value 1

  29. 29

    Changing global variable value inside function

HotTag

Archive