Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > visibility of variable

Reply
Thread Tools

visibility of variable

 
 
rahul8143@gmail.com
Guest
Posts: n/a
 
      09-26-2005
hello,
To limit scope of a variable in a single file that is part of a
large project that have several C files we use static variable
right?then to limit any variable to function scope it should be
declared as auto or anything else?

 
Reply With Quote
 
 
 
 
Julienne Walker
Guest
Posts: n/a
 
      09-26-2005
> To limit scope of a variable in a single file that is part of a
> large project that have several C files we use static variable
> right?


Right.

> then to limit any variable to function scope it should be
> declared as auto or anything else?


auto is the default for local variables, you don't need to do anything
special if you want a variable to be local to a function, just declare
it inside the function.

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-26-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> To limit scope of a variable in a single file that is part of a
> large project that have several C files we use static variable
> right?then to limit any variable to function scope it should be
> declared as auto or anything else?


It seems that you are confusing scope and linkage. Any variable
declared outside a function has file scope, regardless of whether
it is declared with `static' or not. However, `static' gives the
variable internal linkage, which means that it will not be linked
against file-scope identifiers in other translation units.

Variables declared inside a function have block scope, not
function scope. (Only labels have function scope.) When they
are declared without a storage class specifier, or with any
storage class specifier other than `extern', they have no
linkage.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-26-2005
(E-Mail Removed) wrote on 26/09/05 :
> To limit scope of a variable in a single file that is part of a
> large project that have several C files we use static variable
> right?then to limit any variable to function scope it should be
> declared as auto or anything else?


There is no 'function scope'. There is 'block scope'. A variable
defined in a block has the block scope. If it has the 'static'
qualifier, it becomes persistent.

But these methods are not encouraged, specially for large project.
Better to work with 'contexts', where data are defined in a structure,
and where the code is designed to process the data.

It's the first step to Object Oriented Programming, that is a Good
Thing.

Next step is 'ADT' (Abstract Data Types). Google is your friend.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      09-26-2005
Emmanuel Delahaye wrote:
> (E-Mail Removed) wrote on 26/09/05 :
>
>> To limit scope of a variable in a single file that is part of a
>> large project that have several C files we use static variable
>> right?then to limit any variable to function scope it should be
>> declared as auto or anything else?

>
> There is no 'function scope'.


There _is_ function scope.
Function scope is what labels are visible in.

> There is 'block scope'. A variable defined
> in a block has the block scope. If it has the 'static' qualifier, it
> becomes persistent.


Right.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-26-2005
Michael Mair wrote on 26/09/05 :
> Emmanuel Delahaye wrote:
>> (E-Mail Removed) wrote on 26/09/05 :
>>
>>> To limit scope of a variable in a single file that is part of a
>>> large project that have several C files we use static variable
>>> right?then to limit any variable to function scope it should be
>>> declared as auto or anything else?

>>
>> There is no 'function scope'.

>
> There _is_ function scope.
> Function scope is what labels are visible in.
>
>> There is 'block scope'. A variable defined in a block has the block scope.
>> If it has the 'static' qualifier, it becomes persistent.

>
> Right.
>
> Cheers
> Michael


labels ? goto ? Sorry, I don't code in assembly nor in BASIC anymore



--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-26-2005


Emmanuel Delahaye wrote On 09/26/05 15:33,:
> Michael Mair wrote on 26/09/05 :
>
>>Emmanuel Delahaye wrote:
>>
>>>(E-Mail Removed) wrote on 26/09/05 :
>>>
>>>
>>>> To limit scope of a variable in a single file that is part of a
>>>>large project that have several C files we use static variable
>>>>right?then to limit any variable to function scope it should be
>>>>declared as auto or anything else?
>>>
>>>There is no 'function scope'.

>>
>>There _is_ function scope.
>>Function scope is what labels are visible in.
>>
>>
>>>There is 'block scope'. A variable defined in a block has the block scope.
>>>If it has the 'static' qualifier, it becomes persistent.

>>
>>Right.
>>
>>Cheers
>> Michael

>
>
> labels ? goto ? Sorry, I don't code in assembly nor in BASIC anymore
>


Do you write functions with non-empty argument lists?

--
(E-Mail Removed)

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-26-2005
Eric Sosman wrote on 26/09/05 :
>> labels ? goto ? Sorry, I don't code in assembly nor in BASIC anymore
>>

>
> Do you write functions with non-empty argument lists?


Yes, I do.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"


 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-26-2005
Eric Sosman <(E-Mail Removed)> writes:

> Emmanuel Delahaye wrote On 09/26/05 15:33,:
>> Michael Mair wrote on 26/09/05 :
>>
>>>Emmanuel Delahaye wrote:
>>>
>>>>There is no 'function scope'.
>>>
>>>There _is_ function scope.
>>>Function scope is what labels are visible in.
>>>
>>>>There is 'block scope'. A variable defined in a block has the block scope.
>>>>If it has the 'static' qualifier, it becomes persistent.

>>
>> labels ? goto ? Sorry, I don't code in assembly nor in BASIC anymore
>>

>
> Do you write functions with non-empty argument lists?


Irrelevant. "A label name is the only kind of identifier that
has function scope." (C99 6.2.1)

You are thinking of function prototype scope.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      09-27-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> hello,
> To limit scope of a variable in a single file that is part of a
> large project that have several C files we use static variable
> right?then to limit any variable to function scope it should be
> declared as auto or anything else?
>


Related - is it bad form to actually use 'auto' - is being explicit best?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning variable visibility Niklasro(.appspot) Python 1 09-06-2010 03:40 PM
Templates, inheritance and variable visibility ajhietanen@gmail.com C++ 8 04-04-2009 08:01 AM
How do I make a table cells visibility depend on a session variable? COHENMARVIN ASP .Net 2 09-12-2005 08:09 PM
Variable Scope / Visibility / Caching Problem Steve Wark ASP .Net 1 05-23-2005 12:36 PM
Variable Scope / Visibility Steve Wark ASP .Net 0 05-23-2005 12:00 PM



Advertisments