eHow launches Android app: Get the best of eHow on the go.

How To

How to Declare a Static Variable in C

Contributor
By eHow Contributing Writer
(11 Ratings)

Static variables are a useful part of C. They give you flexibility in making functions which return more complex data, but they can be tricky to understand and use effectively.

Difficulty: Moderately Challenging
Instructions

    Declare a Static Variable

  1. Step 1

    Know that static variables are always declared inside a C function, but unlike other variables, their values continue to exist and are retained, even after the function exits.

  2. Step 2

    Declare a static variable by using the same syntax as you would to declare a normal local variable, but precede the declaration with the word static, like this:

    static int sum = 0;

  3. Step 3

    Expect initialization to happen only the first time you call the function. Subsequent times, the previous value will still be there. If you omit the initialization, it will automatically be initialized to 0.

  4. Step 4

    Use the variable in the function like you would any other.

  5. Step 5

    Remember that, like any other local variable, a static variable cannot be referred to outside the function. However, if you pass out a pointer to it, the pointer can be dereferenced successfully, since the variable still exists.

  6. Know When to Use Static Variables

  7. Step 1

    Use a static variable to allow your function to have its own memory that carries over from one call to another. For example, a function that gets and parses the next line of a file might need to internally keep track of where it is in the file.

  8. Step 2

    Use a static variable as a way to provide a piece of memory for storing a result. For example, a function to concatenate strings might use a static variable in which to store the result of the concatenation and return a pointer to it. The static variable's memory is constantly available, but will automatically be freed when the program ends, just like any other local variable.

  9. Step 3

    Use static variables for a running total or similar accumulation. Consider this example:

    int running_total(int num) {
    static int sum = 0;
    sum += num;
    return sum;
    }
    Each time you call this function, it keeps and returns a running total of all numbers passed into it.

Tips & Warnings
  • Use static variables to avoid having to pass a variable into the C function for no reason other than to update and maintain its value.
  • Be careful of side effects when you're returning a pointer to the static variable. If you call the function several times, the result of each is overwritten during the next.
  • It's dangerous to use a pointer to the static variable as a parameter to the function. For example, in a function to concatenate strings, don't try to concatenate something to the result of a previous concatenation!

Comments  

ashishk said

Flag This Comment

on 9/5/2009 Global variables can also be made static, if one doesn't what that they are accessed from outside the file.

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

Demand Media
eHow_eHow Technology and Electronics