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

scerrecrow

My question is in the foo() function, the sa variable seems to be declared and initialized there, however since it is static is it ignored by the compiler after the first time? How come it is not initialized back to the value 10 even if it's static?

#include <stdio.h>

void foo()
{
int a = 10;
static int sa = 10;

a += 5;
sa += 5;

printf("a = %d, sa = %d\n", a, sa);
}


int main()
{
int i;

for (i = 0; i < 10; ++i)
    foo();
}

This prints:

a = 15, sa = 15
a = 15, sa = 20
a = 15, sa = 25
a = 15, sa = 30
a = 15, sa = 35
a = 15, sa = 40
a = 15, sa = 45
a = 15, sa = 50
a = 15, sa = 55
a = 15, sa = 60
Fiddling Bits

Your program would work identically if you declared sa globally, though its scope would be different:

int sa = 10;

void foo()
{
    int a = 10;

    a += 5;
    sa += 5;

    printf("a = %d, sa = %d\n", a, sa);
}

The reason you might want to declare sa within foo is to limit its access.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Can I initial a static global variable by calling a function in C language?

From Dev

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

From Dev

Cannot increment static variable within static class function

From Dev

In C language, Static Variable with pointer access

From Dev

"Undefined reference" to declared C++ static member variable

From Dev

In C++, what is the benefit of defining a Static Const variable within a member function?

From Dev

c function return static variable

From Dev

Calling a function within fprintf syntax in C language

From Dev

Functions declared within a function in C, are they usable at a global scope?

From Dev

static member function with C language binding?

From Java

If a C function is called twice will it create a variable declared in the function twice?

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

c++ template function error: variable or field 'swapAdjacent' declared void

From Dev

Define a static variable in a function like c++

From Dev

C++ static variable in member function

From Dev

Define a static variable in a function like c++

From Dev

C++ static variable in member function

From Dev

Static variable in a function called repeatedly C

From Dev

'declared as a function' in C

From Dev

'declared as a function' in C

From Dev

Function returning a variable without declared it

From Dev

Using variable/function before it is declared

From Dev

Function returning a variable without declared it

From Dev

function access variable declared in decorator

From Dev

variable or field (function) declared void

From Dev

C++ : Using a static member function within class

From Dev

C language : why when input a float number in an int declared variable the result varries

Related Related

  1. 1

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

  2. 2

    Can I initial a static global variable by calling a function in C language?

  3. 3

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

  4. 4

    Cannot increment static variable within static class function

  5. 5

    In C language, Static Variable with pointer access

  6. 6

    "Undefined reference" to declared C++ static member variable

  7. 7

    In C++, what is the benefit of defining a Static Const variable within a member function?

  8. 8

    c function return static variable

  9. 9

    Calling a function within fprintf syntax in C language

  10. 10

    Functions declared within a function in C, are they usable at a global scope?

  11. 11

    static member function with C language binding?

  12. 12

    If a C function is called twice will it create a variable declared in the function twice?

  13. 13

    Can instance variable be declared as static variable in java

  14. 14

    Can instance variable be declared as static variable in java

  15. 15

    c++ template function error: variable or field 'swapAdjacent' declared void

  16. 16

    Define a static variable in a function like c++

  17. 17

    C++ static variable in member function

  18. 18

    Define a static variable in a function like c++

  19. 19

    C++ static variable in member function

  20. 20

    Static variable in a function called repeatedly C

  21. 21

    'declared as a function' in C

  22. 22

    'declared as a function' in C

  23. 23

    Function returning a variable without declared it

  24. 24

    Using variable/function before it is declared

  25. 25

    Function returning a variable without declared it

  26. 26

    function access variable declared in decorator

  27. 27

    variable or field (function) declared void

  28. 28

    C++ : Using a static member function within class

  29. 29

    C language : why when input a float number in an int declared variable the result varries

HotTag

Archive