• Advertisement
Sign in to follow this  

static float?

This topic is 3972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone give me a simple define on: "static" float and why/ or when I would use it. The code below made me rethink what a static is. (i understand the code)

// code from "How not to program in C++" book

1 /***********************************************
  2  * Test question:                              *
  3  *     What does the following program print?  *
  4  *                                             *
  5  * Note: The question is designed to tell if   *
  6  * the student knows the difference between    *
  7  * automatic and static variables.             *
  8  ***********************************************/
  9 #include <stdio.h>
 10 /***********************************************
 11  * first -- Demonstration of automatic         *
 12  *      variables.                             *
 13  ***********************************************/
 14 int first(void)
 15 {
 16     int i = 0; // Demonstration variable
 18     return (i++);
 19 }
 20 /***********************************************
 21  * second -- Demonstration of a static         *
 22  *      variable.                              *
 23  ***********************************************/
 24 int second(void)
 25 {
 26     static int i = 0;  // Demonstration variable
 28     return (i++);
 29 }
 31 int main()
 32 {
 33     int counter;          // Call counter
 35     for (counter = 0; counter < 3; counter++)
 36         printf("First %d\n", first());
 38     for (counter = 0; counter < 3; counter++)
 39         printf("Second %d\n", second());
 41     return (o);
 42 }

Share this post

Link to post
Share on other sites
A standard variable (assumed to be "auto" storage class unless you explicitly state "static") is re-initialized every time the function is called, and is set to garbage unless you specify an initial value.

A static variable is only initialized once - when the function is first called - and defaults to zero unless you specify an initial value. Static variables are good for (among other things) profiling your code by determining how many times a given function was called.

Share this post

Link to post
Share on other sites
Function static variables don't lost their values between calls, they only get initialised the first time the function gets called. Automatic variables only exist when the function is called and get a new initial value every time.

They are mainly a relic from C, as C++ can use function objects to have the same (or better) effect.

An example would be a random generator function. A simple random function would have a seed static variable and would compute its return value by shuffling the bits of the seed and storing the result. The seed could be first initialised to a timer value to provide a reasonably unique seed.

int myRand()
static int seed = time(NULL);
seed = bitshuffle(seed);
return seed;

Most real random generators will have the seed outside the function to allow one to manually set the seed, but this is the general idea.

In c++ (your code is C but the book mentions c++) you could use a function object which has the same advantages (retain the seed state between calls) but you can have more than one seed state active:

class MyRand
MyRand( int initSeed )

int operator()()
seed = bitshuffle(seed);
return seed;
int seed;

// use
void foo()
MyRand myRand( time(NULL) );

int somevalue = myRand(); // use like a function

Function static variables are rare, after all they are a special form of global. In addition to this, they are not thread safe. When to use them? Static variables are one of those features of C++ that once you know what they are, you know when to use them. But do keep in mind that function objects are usually the better choice ( they can be made thread local for example ).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement