hi iam having small doubt.
what is the difference between static and static global variable.
could some body clarify.
hi iam having small doubt.
what is the difference between static and static global variable.
could some body clarify.
static local var is visible only in the function where it is declared
static "global" var is visible in any function of the current source file
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
thank you for reply
A bit further expanation. A static variable defined in a function is different
from a normal variable defined in a function because a static variable will not be
freed at the end of the function. This allows its reference or pointer to be returned from
the function and it will have the same value the next time you call the function.
In other words its like a global variable that can only be accessed inside the functionsCode:int& something() { static int myVar1; //will not be freed at end of function int myVar2; //will be freed at end of function so you cannot return its reference. printf("%i\n", myVar1); //The first time the function is called this may print junk. The next time it should print 32 myVar1 = 32; myVar2 = 32; return myVar1; }
scope and through its return value.
A global variable is defined outside of a function, class, structure, etc.
Code:int myGlobal = 32; void something() { printf("%i\n", myGlobal); //prints 32 }
I dont think it matters if you put the word static in front of a global variable because
it should always be static (static, as in it wont be freed until the end of the program).
EDIT: As mentioned below, static does in fact affect global variables. It limits its scope to the C file it is defined in, meaning you can not use that variable in another C file, even if you use "extern".
Last edited by 39ster; 01-14-2008 at 07:00 PM.
Is there a difference in where the two are stored in memory?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
As mentioned, static before the global makes its linkage internal. That is, you can't access it from other .c files. If you don't add "static" before the global, then its linkage will be external and it can be accessed from other .c files.
Note that the opposite applies to C++! You have to specifically declare a global variable as extern to make it external linkage.
Not to mention, global variables!
What I meant is,
External linkage in C. Can be accessed from multiple .c files.Code:int x = 5;
In C++, however, it's internal linkage so trying to access it from another .cpp file will yield a compile error and redefining it will yield a linking error.
Internal linkage in C. Cannot be accessed from other .c files.Code:static int x = 5;
I don't know what the effect is in C++, though. I don't know if it will cause external linkage in this case.
If we add this in C++, THEN it's external linkage.Code:extern int x;
But to get external linkage in C, don't you need to do extern in the other file you want to use it in?
Well, I'm not a C programmer, but try this:
Do something like:
int x = 5;
In one .c file.
Then print the value in another .c file with extern:
extern int x;
It will work.
Now try adding
static int x = 5;
and then
extern int x;
Oops, linking error.
The only thing I know is that the same example works in C++ and causes an error as well
Yeah, someone has explained the linkage before, but I don't think I'm an expert at it.
Yeah exactly, so what's the difference?
Other C files cannot access variables that have been defined as static. Only the C file that declared the variable can access it even if you use extern in the other C file -_-.
Havnt tried it but it might allow you to create variables with the same name and definition in different C files.
'global' variables declared in seperate files have file scope only. Specifying them as static does nothing, as global variabels remain in scope until the application terminates. You can get away with reusing the same variable name in different files and it will have a different memory location for each copy, btu then you cannot use extern. Even a single extern will cause problems as your compiler will either randomly select which one to use, or toss an error about redefinition.
Declaring a varible as static within a function can have ramifications for reentrant code, so be careful if multiple threads access functions with static variables. This can lead to useful or horrendous behavior depending on how they are used.
>Even a single extern will cause problems as your compiler will either randomly select which one to use, or toss an error about redefinition.
no, extern prevents redefinition errors, and the one that doesn't have the extern will be the one that is used.
Elysia:Am I missing something?Code:C:\>type f1.c int x; extern int y; void func(); int u; int main() { } C:\>type f2.c extern int x; static int y; void func() { u ++; return; } C:\>gcc f1.c f2.c -Wall -Werror -pedantic -std=c99 f2.c: In function `func': f2.c:7: error: `u' undeclared (first use in this function) f2.c:7: error: (Each undeclared identifier is reported only once f2.c:7: error: for each function it appears in.) f2.c: At top level: f2.c:3: warning: 'y' defined but not used C:\>move f1.c f1.cc C:\>move f2.c f2.cc C:\>g++ f1.cc f2.cc -Wall -Werror -pedantic f2.cc: In function `void func()': f2.cc:7: error: `u' undeclared (first use this function) f2.cc:7: error: (Each undeclared identifier is reported only once for each funct ion it appears in.) f2.cc: At global scope: f2.cc:3: warning: 'y' defined but not used C:\>