What is the use of Static local variable when we can get a global variable at the same cost?

28,919

Solution 1

You write that a global variable has a “better” scope. This is incorrect. It has a bigger scope. Bigger is not better.

Bigger may be necessary, if you need an identifier to be visible in more places, but this is often not the case. But a bigger scope means more exposure to errors. Global variables muddle the semantics of routines by making it harder to see what program state they use and change, and it increases the probability of errors caused by failing to declare a local identifier and of other errors.

In particular, an identifier with external linkage will collide with identifiers in other libraries. Consider what happens when you are writing a physics application, have an external identifier named acceleration, and link with a physics library that also has an external identifier named acceleration. The program will fail. Because of this, external identifiers are usually bad design.

A significant limit on our ability to develop and maintain complex software is human error. Much of programming language semantics limits the language to prevent errors. With a raw computer, you can add two pointers, trash your stack pointer, accidentally load the bytes of a float into an integer register, and so on. Good programming languages make these errors difficult to do by mistake.

Global variables were a larger source of errors before scoping rules helped control them. Good programmers limit the scopes of their identifiers.

Solution 2

A global variable is well, global, it can be accessed from anywhere.

A static local variable has local scope. It is static, so it's lifetime runs across the lifetime of the application however it can only be accessed from the local scope (whether that scope is a function, a block, or a file)

Solution 3

The basic difference is on scope of variable.

1) global variable is global for entire project. lets say your project has 10 different files then all 10 files can access the global variable(see how to use extern).

2) static variable/function can be used by function/file within which it is defined. It cannot be used by any other file in your project.

yet, you can modify the static variable(defined in func1()) in func2() by passing reference of the variable. please look into below example,

void func2(int *i)
{
    (*i)++;
}

void func1()
{
    static int i;

    i=1;
    printf("%d\n", i);
    func2(&i);
    printf("%d\n", i);  
}

int main()
{
    func1();
    return 0;
}

As you see above, func1() has static int i which cannot be directly manipulated by func2(), but if you pass reference of the variable you can still manipulate the variable like ordinary variable.

hope it helps...

Share:
28,919
Adi
Author by

Adi

Updated on June 20, 2020

Comments

  • Adi
    Adi almost 4 years

    In C ,what is the use of static storage class when an external variable can serve its purpose at the same cost ie. both occupy storage space in the data segment of the executable.

    I have much better scope with external variable.If i want the scope of external variable to be specific file i do not declare this variable else where.i see a lot of flexibility with a global variable that static local variable

    And we can refer to local static variable outside the function if we have the address of the variable.Memory for local static variable will be in Data segment not in the stack frame of the function.So unique feature does static storage class bring to the table.

    I just want to know whether static has any subtle purpose that i m not aware of.

    • Koushik Shetty
      Koushik Shetty about 11 years
      well did you not consider scope as the 1st possible explanation. second would be to keep things private. static does that
    • stijn
      stijn about 11 years
    • Adi
      Adi about 11 years
      @ koushik :I have much better scope with external variable.If i want the scope to be specific file i do not declare this variable else where.i see a lot of flexibility with a global variable that static local variable.
    • unkulunkulu
      unkulunkulu about 11 years
      Bigger doesn't mean better, sometimes limitations are a great thing.
    • autistic
      autistic about 11 years
      There are no globals in C. What you're referring to is a variable declared at file scope (which happens to implicitly have static storage duration, by the way).
    • Koushik Shetty
      Koushik Shetty about 11 years
      flexibility is in the context of design. suppose i want a variable for one translation unit but hide it from the other?.since you mention static local it means you only the block which declares it can access it.
    • Adi
      Adi about 11 years
      @ unkulunkulu : I agree sometimes limitations are a great thing,but flexibility is always better.
    • Adi
      Adi about 11 years
      @ koushik:I think Other blocks also can access a static local variable if the present block returns the address to other blocks.Cant they?
    • Koushik Shetty
      Koushik Shetty about 11 years
      yeah surely they can. the variable is still alive and it is safe if you just want the rvalue.
    • Koushik Shetty
      Koushik Shetty about 11 years
      it is safe even if you want the lvalue
    • M.M
      M.M over 7 years
      "external variable" also has static storage class. I guess you mean, what is the use of internal linkage.
  • Adi
    Adi about 11 years
    we can refer to local static variable outside the function if we have the address of the variable.Memory for local static variable will be in Data segment not in the stack frame of the function.
  • Adi
    Adi about 11 years
    we can refer to local static variable outside the function if we have the address of the variable.Memory for local static variable will be in Data segment not in the stack frame of the function.cant we?
  • Adi
    Adi about 11 years
    we can refer to local static variable outside the function if we have the address of the variable.Memory for local static variable will be in Data segment not in the stack frame of the function.cant we?
  • msam
    msam about 11 years
    true, if you have the memory address then you have access but doesn't this apply to any variable? even with local non static variables you could conceivably access the memory location from another thread. The language provides static so that you can limit the scope of that variable not of a particular memory location
  • Adi
    Adi about 11 years
    Unfortunately this doesnt help,I know this already..my question is what are the features that static can uniquely provide that extern cant.
  • msam
    msam about 11 years
    @BillHicks additionally, static at filescope allows you to have the same variable name at file level representing different memory locations in different compilation units while non static variables at file scope with the same name share the same memory (this is an additional feature brought by static).
  • Kinjal Patel
    Kinjal Patel about 11 years
    well you can restrict your functions within specific file using static... while you can access functions defined in one file from any other file by referencing the function prototype using extern.....thats primary
  • unkulunkulu
    unkulunkulu about 11 years
    If we have an address, we can access anything in C, even something that is not a variable and doesn't have a name in the code. So yes. The scope is where we can access the variable by its name, so in this sense the scope is limited to the function where it's defined. On the memory part you're correct, no stack of course.
  • Matthew Read
    Matthew Read almost 11 years
    @BillHicks No. An address is not a variable.
  • Toby Speight
    Toby Speight almost 6 years
    This doesn't explain why we prefer the local static to the global (although it begins to, by showing how local() modifies the global a).