-2

I want to know what is the difference between declaring a static variable locally and globally, because in below example.

If results do not impact, then why we have two scopes for static?

#include <stdio.h>

static int y = 0;
int main()
{
    int x;
    x = func_call();
    printf("x is %d\n",x);
    x = func_call();
    printf("x is %d\n",x);

    return x;
}

int func_call()
{
    y++;
    return y;
}


#include <stdio.h>
    
int main()
{
    int x;
    x = func_call();
    printf("x is %d\n",x);
    x = func_call();
    printf("x is %d\n",x);

    return x;
}

int func_call()
{
static int y = 0;
    y++;
    return y;
}
Ian Abbott
  • 15,083
  • 19
  • 33
krupa n
  • 1
  • 2
  • Memory, a global variable will always use more memory than a local one – SoyNeko Feb 27 '23 at 12:53
  • Please see e.g. [this reference about storage duration and linkage](https://en.cppreference.com/w/c/language/storage_duration). – Some programmer dude Feb 27 '23 at 12:53
  • And like many other things in C, the actual meaning of `static` depends on context. Take for example the asterisk `*` which can mean multiplication, pointer declaration, or pointer dereference depending on context. It's the same with `static`: In block-scope (inside a function) it means one thing, in file-scope (the "global" scope) it means something else. – Some programmer dude Feb 27 '23 at 12:55
  • 2
    @SoyNeko a static variable has exactly the same memory requirement. – Weather Vane Feb 27 '23 at 13:01
  • @SoyNeko: Re “Memory, a global variable will always use more memory than a local one”: No, it will not. – Eric Postpischil Feb 27 '23 at 13:21
  • @EricPostpischil was my memory from my studies, thanks for change my mind ! – SoyNeko Feb 27 '23 at 13:22
  • @krupan See [this related answer](https://stackoverflow.com/questions/51329671#51329933). – Steve Summit Feb 27 '23 at 13:43

4 Answers4

2

A variable declared static, whether in a function or file scope, has full-program lifetime. The difference is a matter of scope, i.e. where the name can be used.

For a static variable declared at file scope, it can be used directly anyplace in the file where it was declared (or more accurately, anyplace after it was declared). For a static declared in a function, it can only be used by that name from the point it was declared until the closing brace of the scope it is in, however it's still possible to reference it outside that scope via a pointer.

dbush
  • 205,898
  • 23
  • 218
  • 273
1
  1. Static variable in the global scope. Its lifetime is the same as the whole program. It does not have external linkage so they cant be accessed (but you can use a reference to it) outside its compilation unit.

  2. Static automatic variable is only visible in the function (where it was defined) scope. It maintains the value between the function calls. It has a static storage duration and it is zeroed if not initialized. You can safely return a reference to this variable.

example:

int *foo(void)
{
    static int x;

    printf("%d\n", x++);
    return &x;
}

int main(void)
{
    int *ptr;
    ptr = foo();
    printf("accessed by pointer: %d\n", *ptr);
    ptr = foo();
    printf("accessed by pointer: %d\n", *ptr);
    ptr = foo();
    printf("accessed by pointer: %d\n", *ptr);
}

https://godbolt.org/z/s18oxhnhb

0___________
  • 60,014
  • 4
  • 34
  • 74
0

The only difference between a true global variable (definition at file scope with external linkage), a file-scope static variable (definition at file scope with internal linkage), and a function-scope static variable, is the set of locations from which the variable can be referred to: the entire program, the file it's declared in, and the function it's declared in, respectively.

The point of the feature of scope control for globals—the reason the language offers all three of these possibilities—is that it lets you be sure that no code outside a limited scope can access. a variable, and it lets you have two variables with the same name as long as their scopes do not intersect.

zwol
  • 135,547
  • 38
  • 252
  • 361
0

The static keyword is "overloaded" to refer to both static linkage and static storage depending on the context in which it is used.

All globals have static storage class for in any event, so outside of a function, it refers to linkage and makes the symbol inaccessible outside of the translation unit it is defined in.

Locals cannot have external linkage, and in that context static is a storage-class specifier. It means that the variable continues to exist and retain its value outside the scope in which it is declared and is accessible.

Note that whilst static linkage applies to variable and function symbols, static storage class applies to variables only.

Clifford
  • 88,407
  • 13
  • 85
  • 165