Describe about storage allocation and scope of global, extern, static, local and register variables?

4 replies [Last post]
dhanunjaya
Offline
Last seen: 10 years 4 weeks ago
Joined: 2009-04-26
bOt Points: 153

Describe about storage allocation and scope of global, extern, static, local and register variables?

nitesh
Offline
Last seen: 9 years 35 weeks ago
Joined: 2009-03-26
bOt Points: 370
my most humble request is

my most humble request

is that a question you are asking or ORDERING a description

mike
Offline
Last seen: 10 years 48 weeks ago
Joined: 2009-03-31
bOt Points: 66
@nitesh

@nitesh

 

sarcasm............

 

that doesnt seem to be way of putting up a question

codeguru
Offline
Last seen: 10 years 44 weeks ago
Joined: 2009-04-08
bOt Points: 114
everything else already been

everything else already been pointed , now i can get to the point

 

SCOPE

global -across all files and function of the program/project

static,local,register - scope same as for auto i.e limited to the declaring block

 

in case  of a global static the scope is restricted in the abovementined manner i.e. to the declaring block i.e the file which declares the variable

 

extern - the use of extern keyword restricts definiton of the variable to a declaration. Hence it does not actually modify the scope of that variable. It simply tells the compiler that somewhere in the current program and in the current scope there is a variable (say a) and it should use that variable .

Thus if we try to define a variable after its extern declaration within the same block it results in a linker error.

again a variable declared as extern in any program should BE DEFINED ata some or the other place in the program .

 

 

 

STORAGE ALLOCATION

though i have already clarified on this issue i just recall what i already said.

global -data  segment ==> data/BSS

static -data segment==> BSS

register - CPU registers

local/auto (not global) - stack

 

LIFETIME

global ,static - program run

register, local/auto  - until end of function /block

 

 

rupakgpt
Offline
Last seen: 10 years 18 weeks ago
Joined: 2009-04-08
bOt Points: 4
extern variable

External References: extern

* If a variable is declared (with global scope) in one file but referenced in another, the extern keyword is used to inform the compiler of the variable's existence: