English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

4 answers

Static variables are created and initialised at compile time.
Dynamic variables are created and initialised at run time.

So if you want to keep your program small for fast loading then use dynamic variables.

If you want your program to run faster but don't mind waiting for the load time then use static variables.

Of course this all depends on the speed of the machine that the program is running on. These days it does not actually matter that much.

2007-08-01 01:06:30 · answer #1 · answered by AnalProgrammer 7 · 0 0

the question is very strange. do you want to know the difference of static variables and non-static variables in a function, or in a module?

in a function:
int f()
{
static int s;
int i;
}

s is static, i is not. i is reinitialized whenever f is called. when f() returns, i loses its value. s is only initialized once, and never loses the value.

in a module/C file:
static variables have a scope which is limited to the C file. non-static variables can also be used from other C files (but you usually need a declaration as "external").

BTW - i don't know of any performance difference between static and non-static variables. static variables are initialized only once, while non-static variables are allocated on the stack whenever their function is called. therefore, the stack pointer has to be incremented, which costs you a nanosecond on a processor with 1 GHz, and this is completely irrelevant.

2007-08-01 02:34:00 · answer #2 · answered by cruppstahl 4 · 0 0

Static variables hold their state when they go out of scope.

Dynamic variables are faster than static variables, as they will be stored in registers and the compiler can perform more optimizations with them. Static vars are stored on the heap.

2007-08-03 11:19:30 · answer #3 · answered by gam3fr3aks 3 · 0 0

The clue's in the name.

2007-08-01 01:03:36 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers