-
Junior Member
What is the difference between static and global variables
What is the difference between static and global variables? for Ex: declare a global parameter : int a static parameter : static int a;
What is the difference between them.
Can someone explain me clearly?
Waiting for early reply
-
Junior Member
Re: What is the difference between static and global variables
the global variable has a global scope and life time of the whole program..while
a static variable has a local scope(can't be used outside the scope) but life time of the whole program( its value persists even if the function returns)..
i think u got that...??
-
Contributing Member
Re: What is the difference between static and global variables
The memory is allocated for static variables only once i.e all the objects shared by same memory but when it comes to global variables memory is allocated for each and every object.
Regards,
hari prasad
-
Junior Member
Re: What is the difference between static and global variables
hi, may be its too late to answer, if u are not yet clear, then here is bit more info...
global variable has global scope, i mean it can be accessed by any function, from any file. where as static variable has file scope, it is not possible to access the variable from any other file. This technique is helpfull when u want to make, varaible accessible to all functions of that file, but not functions of other file.
-
Junior Member
Re: What is the difference between static and global variables
Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).
Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be
accessible only in this file (file scope).
On the other hand global variables have to be defined globally, persists (life is) throughout the program, scope is also throughout the program. This means such variables can be accessed from any function, any file of the program.
So if you have a global variable and u r distributing ur files as a library and you want others to not access your global variable, you may make it static by just prefixing keyword static (of course if same variable is not required in other files of yours).
-
Junior Member
Re: What is the difference between static and global variables
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules