visibility of variable

R

rahul8143

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?
 
J

Julienne Walker

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.
 
B

Ben Pfaff

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.
 
E

Emmanuel Delahaye

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."
 
M

Michael Mair

Emmanuel said:
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

Emmanuel Delahaye

E

Eric Sosman

Emmanuel Delahaye wrote On 09/26/05 15:33,:
Michael Mair 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?
 
B

Ben Pfaff

Eric Sosman said:
Emmanuel Delahaye wrote On 09/26/05 15:33,:

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.
 
P

pemo

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?
 
K

Keith Thompson

pemo said:
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.
 
P

pemo

Keith Thompson said:
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.
 
C

Christopher Benson-Manica

pemo said:
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.
 
F

Flash Gordon

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.
 
K

Keith Thompson

pemo said:
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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top