Static vs. global

Hi, can anyone tell me the difference between a static and global variable and how they effect the stack in the "C" language.

Thank You

Jim

Reply to
jg
Loading thread data ...

Within the scope of a function a static variable persists between function calls just like any other global (it is not on the stack). However it is only visible from within the function. The function could return a pointer to the variable and it would be vaild when the function has returned.

Within the scope of a file, a static variable is like any other global EXCEPT that it cannot be seen by the linker and cannot be accessed by another module using the "extern" keyword. Again pointers to static variables can be passed about - static does not make them impossible to get at from outside a module just impossible to see using "extern".

try:

void fred(int val) { static int cnt = 0;

printf("%d %d\n", cnt++, val);

}

main() { int j;

for (j=3; j

Reply to
Sergio Masci

Thanks for the explanation

Jim

Sergio Masci wrote:

Reply to
jg

jg wrote in news:8dzYb.56208$ snipped-for-privacy@twister.tampabay.rr.com:

Think of it this way, static always means the variable exists for the life of the program and its scope is restricted to the enclosing file or block, which ever is the tighter scoping.

--
- Mark ->
--
Reply to
Mark A. Odell

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.