Static and Automatic Variables

Status
Not open for further replies.

carrot

Full Member level 3
Joined
Feb 23, 2004
Messages
182
Helped
9
Reputation
18
Reaction score
4
Trophy points
1,298
Location
Bangalore, India
Activity points
1,532
Hi,

Can anyone let me know the difference between Static and Automatic Variables with an example?

Thanks in Advance,
Carrot
 

a static variale value will be available for all in the source.

automatic is dynamic in its scope.
i.e , some parts cannot access its value.
 
Reactions: carrot

    carrot

    Points: 2
    Helpful Answer Positive Rating
Example .

Function 1 has variables A(Equals 20) and S (Equals 25)
A - Auto S - Static Variable.
Function 2 has variable A(Equals 30) defined in it.
Function 3 has no variables declared in it.
In case of Auto, the variable A will have value of 20 or 30 depending on where we are. If we are in Function1, it will have 20 and if we are in Function 2, it will have 30. In function 3, if we try to use variable A, we will get an error indicating "Undefined Variable" because within that function, we have not declared it.

In case of static, S will have the value 25 wherever it is called. No need to even define it..

Regards,
R.Srideepa
 
Reactions: carrot

    carrot

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…