473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

visibility of variable

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?

Nov 15 '05 #1
14 2371
> 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.

Nov 15 '05 #2
ra*******@gmail.com 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
Nov 15 '05 #3
ra*******@gmail.com 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."
Nov 15 '05 #4
Emmanuel Delahaye wrote:
ra*******@gmail.com 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.
Nov 15 '05 #5
Michael Mair wrote on 26/09/05 :
Emmanuel Delahaye wrote:
ra*******@gmail.com 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?"
Nov 15 '05 #6


Emmanuel Delahaye wrote On 09/26/05 15:33,:
Michael Mair wrote on 26/09/05 :
Emmanuel Delahaye wrote:
ra*******@gmail.com 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?

--
Er*********@sun.com

Nov 15 '05 #7
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?"
Nov 15 '05 #8
Eric Sosman <er*********@sun.com> 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
Nov 15 '05 #9

<ra*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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?
Nov 15 '05 #10
"pemo" <us************@gmail.com> writes:
<ra*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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?


There's no reason to use the "auto" keyword. It was useful in early
versions of C, where a variable declaration didn't require a type name
(defaulting to int), so "auto x;" would declare x as an int. In
modern C, the type name is required, so the "auto" keyword is
unnecessary.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...

There's no reason to use the "auto" keyword. It was useful in early
versions of C, where a variable declaration didn't require a type name
(defaulting to int), so "auto x;" would declare x as an int. In
modern C, the type name is required, so the "auto" keyword is
unnecessary.


I realised that Keith, but I was wondering whether it'd be laughed at - if
one actually *used* auto

auto int n = 0;

I believe in being as explicit as is reasonable - so I was wondering what
others thought of this notion.
Nov 15 '05 #12
pemo <us************@gmail.com> wrote:
I realised that Keith, but I was wondering whether it'd be laughed at - if
one actually *used* auto
I don't know if one would be laughed at, but one's understanding of C
might well be called into question. A reviewer might wonder with good
reason what someone who wrote
auto int n = 0;
expected

int n=0;

to do.
I believe in being as explicit as is reasonable


It's a common and noble goal. The keyword "auto" has become
superfluous to the extent that using it obfuscates more than it
reveals. A student coming from a poorer C background might be unaware
of its existence.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #13
pemo wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
There's no reason to use the "auto" keyword. It was useful in early

<snip>
I realised that Keith, but I was wondering whether it'd be laughed at - if
one actually *used* auto

auto int n = 0;

I believe in being as explicit as is reasonable - so I was wondering what
others thought of this notion.


Personally, if I saw it at a review I would tell you to delete it. I
might also ask you what you were smoking when you put it in since no one
else does.

To me, the natural assumption with any language is that the scope of any
declared item will be the scope at which it is defined unless there is
something explicit overriding it. So why would one even consider
specifying that?

Actually, I would prefer it if things defaulted to not having external
linkage as well, but that is not how the language was defined.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #14
"pemo" <us************@gmail.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...

There's no reason to use the "auto" keyword. It was useful in early
versions of C, where a variable declaration didn't require a type name
(defaulting to int), so "auto x;" would declare x as an int. In
modern C, the type name is required, so the "auto" keyword is
unnecessary.
I realised that Keith, but I was wondering whether it'd be laughed at - if
one actually *used* auto

auto int n = 0;


Yes, you would be laughed at. I'm barely able to contain my giggles.
I believe in being as explicit as is reasonable - so I was wondering what
others thought of this notion.


In this particular case, that's more explicit than is reasonable, even
more so than

(void)printf("Hello, world\n");

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jonathan | last post by:
Hi, I've read through quite a number of postings on here so far and have seen what look like very simply, reasonable answers to this question, however I am still completely unable to do what I...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
5
by: Michael Sgier | last post by:
Hello i don't understand the visibility/passing of variables. I've in texture.h: class CTexture { public: unsigned int texID; GLuint texture;
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
1
by: Jim Heavey | last post by:
Hello, I create a HTML line as follows: <P class=text12BoldRed id=ErrTimePeriodSelection style="VISIBILITY: hidden" runat="server">Selection of time period either not made or more then one type...
11
by: -D- | last post by:
How can I turn the visibility of the xml control on or off? <%@ Control Language="c#" AutoEventWireup="false" Codebehind="TopNavBar.ascx.cs" Inherits="compass.user_controls.TopNavBar"...
6
by: jhullu | last post by:
Hi all I'm lookup for a documentation or explanation abour the visibility and syntax about class. I found the ECMAScript Language Spec. 3 (final) & 4 (draft) but it's not very easy to read...
6
by: BOMEz | last post by:
So i've recently been starting to program PHP in an object oriented way, but I'm running into some difficulties in from a design stand point and from an object oriented stand point: Issue 1: In my...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.