Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

difference between static and global

Status
Not open for further replies.

erodeboy

Member level 5
Joined
Nov 15, 2005
Messages
80
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
India
Activity points
1,987
what is the difference between static variable and global variable in c programming
 

idlebrain

Advanced Member level 4
Joined
Jul 20, 2005
Messages
107
Helped
20
Reputation
40
Reaction score
1
Trophy points
1,298
Activity points
2,501
Static and global variable differ a lot in their life and scope.

Static variables are local in scope to the function in which they are defined, but life is throughout the program. A static variable diffined in a function can not be refferenced out side of the function. But the variable is alive in memory through out the program and retains it's value between function calls. A simple local variable is created in the stack always when the function is called. A static variable is initialized in the firt call of the function in which it is deffined.

Golbal variable's scope and life is through out the program so can be accessed from any part of the program (any function or file). The global variable is initialized by the start up code and reatins it's assigned value until next assignment.

If a global variable is deffiened with static, this variable can not be accessed by the other c files of the same program.

Cheers
idlebrain
 

arunragavan

Advanced Member level 1
Joined
Jul 1, 2004
Messages
415
Helped
30
Reputation
60
Reaction score
9
Trophy points
1,298
Location
India
Activity points
5,028
well static varibles.. are those whos values are maintained even thru the functions calls are even between blocks..and value of the static varible cannot be modified.

Memory for static - main memory.
initial value is - zero.
scope - till the program ends.
life - till program ends..


but when u consider global varibles.. they are declared before main and the value of these global varibles can be accessed throughout the program..even in main() as well as functions...

u need to initilize global varibles..try to decalre a global varilble.. and print it without initilizein its value.. if u get some junk.. then kool..

give it a shot..

with regards,
arun
 

sinu_gowde

Full Member level 2
Joined
Nov 10, 2005
Messages
135
Helped
32
Reputation
64
Reaction score
12
Trophy points
1,298
Activity points
3,488
Difference Between Static & Global Variable:

Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and
values are reinitialized. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.

Had it been gloal variable, it would have been available outside the scope of the function, but static variable is not available outside the scope of a function (helpful in localizing errors - as it can't be changed outside the func
scope).
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).

cheers...
 

fdarkangel

Newbie level 6
Joined
Dec 2, 2005
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,398
in a lower level view, a static variable has a fixed location in memory, and compiler orginizes the code so that no other variable uses it (unless you're explicity pointing to it's address). and effect is, it's value is kept during entire life time of the program.
a global variable is simply an exported label. in assembly, a label is local unless it's explictly said to be exported, so that it will take it's place in common symbol table, so that it can be referred within any object file.
 

niket_304

Member level 4
Joined
Dec 21, 2005
Messages
72
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
2,002
ststic is one which havew a conststn valu
eg: static mask 52 means mask have a constsnt value of 52 which cannot be changed. while global is a entirely new thing with the variable having values which can be changed & avan be acessed form the netire project workspace.
bye
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,895
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,879
You can also have static global variables and functions. This is to do with scope, the variables or functions are only visible in the file in which they are declared.
 

Mystry

Newbie level 3
Joined
Feb 25, 2006
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,314
i would like to know that is it necessary to again declare a global variable in a function(other than main)...if yes how do we do that...& can it be initialised in the function everytime the function is called
thanks
 

cae20452

Newbie level 6
Joined
Dec 17, 2005
Messages
13
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,581
hi
Global variables and Static Variables,
Initial difference between these 2 types of variables,
U can reach global variable in anywhere in the code of the program. but this is not true for the static variable(but holding the last value even if exiting the function).
briefly it is so
thanx

Added after 17 minutes:

For the question to define the global variables again in the functions apart from main func.
u dont have to do define them again
global variable is valid in the main scope of codes.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top