Hello,

Here you are right about variable p in add function that it retains it's
value even though function loses its scope. And for main function error you
are seeing has nothing to do with how that variable is stored?
It is about the scope of that variable C compiler sees the scope of static
variable p  limited to add function only, so other function can't use it.
And remember error is at compile time not run time so it is related to
compiler not memory. I hope it explains everything.

On Tue, Feb 7, 2012 at 9:43 AM, atul anand <atul.87fri...@gmail.com> wrote:

> http://www.geeksforgeeks.org/archives/14268
>
>
> On Tue, Feb 7, 2012 at 1:06 AM, gmagog...@gmail.com 
> <gmagog...@gmail.com>wrote:
>
>> I think you are right about p being in BSS segment and it does last even
>> the function finishes, however, you may need a pointer to get the data out
>> of p. Then you can read the data.
>>
>> Correct me if i am wrong
>>
>>
>> On Mon, Feb 6, 2012 at 1:04 PM, Ravi Ranjan <ravi.cool2...@gmail.com>wrote:
>>
>>> i have a confusion in it
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>>
>>> void add(int,int);
>>>
>>> int main(int argc, char *argv[])
>>> {
>>>
>>> add(6,3);
>>> printf("%d",p);
>>>
>>>   system("PAUSE");
>>>   return 0;
>>> }
>>>
>>>
>>> void add(int a, int b)
>>> {
>>>   static int p;
>>> p = a+ b;
>>> }
>>>
>>>
>>> here the memory layout says variable "p" is in BSS segment ... so its an
>>> independent region from stack frame..... when the function looses its scope
>>> from function defination(add) then still it should be alive... and can be
>>> recognized/used by other function(main).... but it gves an error of unknown
>>> variable "p".....  need the correct logic....... if i m wrong...
>>>
>>> thanx
>>> ravi
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Thanks and Regards,
Sumit Mahamuni.

-- Slow code that scales better can be faster than fast code that doesn't
scale!
-- Tough times never lasts, but tough people do.
-- I love deadlines. I like the whooshing sound they make as they fly by. -
D. Adams

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to