Scope of a variable declared in for loop

V

vaysagekv

Hi,
A)Is it possible to declare a variable inside for loop like
for(int i=100;i>=0;i--);

if yes what is the scope of that variable?

B)if there is a for loop like below

for(int i=0;int c = getchar();i++);

after finishing for loop execution c variable will be destroyed or not.
 
A

Alexander Bartolich

vaysagekv said:
[...]
A)Is it possible to declare a variable inside for loop like
for(int i=100;i>=0;i--);

You can do this in C99 and C++, but not in preceding C standards.
Given the low popularity of C99 amongst compiler vendors I recommend
against using its features.
if yes what is the scope of that variable?

According to the standard (both C99 and C++) the scope is restricted to
the loop, i.e. variable »i« is not visible outside the loop. However,
some older C++ compilers did this wrong. Some contemporary compilers
provide an option to switch scoping to the non-standard behaviour.
B)if there is a for loop like below

for(int i=0;int c = getchar();i++);

This code violates the standard. Variable declarations are allowed
only in the first clause of the for-statement, i.e. like this:

for(int c, i = 0; c = getchar(); i++);
after finishing for loop execution c variable will be destroyed or not.

The standard only regulates the scope of the variable.

Whether the memory required by block-local variables is reclaimed at
the end of the block or at the end of the enclosing function is up to
the implementation.

--
 
M

Malcolm McLean

Hi,
A)Is it possible to declare a variable inside for loop like
   for(int i=100;i>=0;i--);

if yes what is the scope of that variable?

B)if there is a for loop like below

   for(int i=0;int c = getchar();i++);

   after finishing for loop execution c variable will be destroyed or not.
The answers depend on what version of C you are using. Also some
people use C++ as what is to all intents and purposes a C compiler
with a few differences, one being that C++ always allows variables to
be declared anywhere.
 
M

Mark Bluemel

Hi,
A)Is it possible to declare a variable inside for loop like
   for(int i=100;i>=0;i--);

Not in C.

Variables can be declared inside a block (i.e. between braces) but not
in the context you are talking about.
if yes what is the scope of that variable?

The question does not arise.
 
S

santosh

Mark Bluemel said:
Not in C.

Variables can be declared inside a block (i.e. between braces) but
not in the context you are talking about.

I'm sure it's just a case of over-looking, but just for lurkers'
benefit, the first code snippet shown by the OP, which you've quoted
above is correct C, though "C99 only", one should qualify.

The second of his for loops is a syntax I believe, since declarations
are only allowed in the initialisation part of the for loop, AFAIK.
The question does not arise.

For the first example, it does arise.
 
S

Seebs

Hi,
A)Is it possible to declare a variable inside for loop like
for(int i=100;i>=0;i--);

In C99, yes. In C89, no.
if yes what is the scope of that variable?

The loop.
B)if there is a for loop like below

for(int i=0;int c = getchar();i++);

after finishing for loop execution c variable will be destroyed or not.

I'm not sure that's allowed off the top of my head -- I thought only the first
clause could be a declaration. But in any event, yes, the variable's scope is
the loop only.

I believe this is now true in C++. I have a published book in which it's
explained that it should have been true, but that there was so much existing
code relying on access to the loop variable after the loop that it could never
be done. :)

-s
 
P

Phred Phungus

Seebs said:
In C99, yes. In C89, no.


The loop.


I'm not sure that's allowed off the top of my head -- I thought only the first
clause could be a declaration. But in any event, yes, the variable's scope is
the loop only.

I believe this is now true in C++. I have a published book in which it's
explained that it should have been true, but that there was so much existing
code relying on access to the loop variable after the loop that it could never
be done. :)

-s

Which book?
 
K

Keith Thompson

Seebs said:
If I remembered, I'd have said. It was probably one of the early editions
of design & evolution of C++ or something similar.

In the 1994 edition of "The Design and Evolution of C++", Stroustrup
says:

Unfortunately, I didn't take the opportunity to change the
semantics to limit the scope of a variable introduced in this
way to the scope of the for-statement. The reason for this
omission was primarily to avoid adding a special case to the
rule that says "the scope of a variable extends from the point
of its declaration to the end of its block".

This rule is the subject of much discussion and may be revised
to match the rule for declarations in conditions.

It was revised in this way, apparently before C99 picked up the
feature. (Declarations in conditions are another C++ feature that
C99 didn't adopt.)
 
S

Seebs

It was revised in this way, apparently before C99 picked up the
feature. (Declarations in conditions are another C++ feature that
C99 didn't adopt.)

In the C99 discussion, we weren't sure which way C++ went, but we
decided to just do the scope correctly regardless, as I recall.

Happily, this turned out not to be an incompatibility.

-s
 
J

jacob navia

Mark Bluemel a écrit :
Not in C.

Variables can be declared inside a block (i.e. between braces) but not
in the context you are talking about.


The question does not arise.

You are completely wrong. Standard C allows that declaration and the scope
is the scope of the for block.

Read the standard before answering please
 
J

jacob navia

Alexander Bartolich a écrit :
vaysagekv said:
[...]
A)Is it possible to declare a variable inside for loop like
for(int i=100;i>=0;i--);

You can do this in C99 and C++, but not in preceding C standards.
Given the low popularity of C99 amongst compiler vendors I recommend
against using its features.

Standard C is implemented in most systems. This person is just talking nonsense
 
S

Seebs

Alexander Bartolich a écrit :
Standard C is implemented in most systems. This person is just talking
nonsense

Off the top of my head, I cannot name a single complete implementation.

On the other hand, I don't think I've used any compiler this millennium which
doesn't support declarations in for loops. It's an especially likely feature
simply because so many C compilers are bundled with C++ compilers, meaning
the developer has almost certainly solved this problem.

-s
 
K

Keith Thompson

jacob navia said:
Alexander Bartolich a écrit :
vaysagekv said:
[...]
A)Is it possible to declare a variable inside for loop like
for(int i=100;i>=0;i--);

You can do this in C99 and C++, but not in preceding C standards.
Given the low popularity of C99 amongst compiler vendors I recommend
against using its features.

Standard C is implemented in most systems. This person is just
talking nonsense

There are plenty of compilers that don't implement, or don't fully
implement, the C99 standard. Support for the C90 standard, on the
other hand, is almost universal.

On the other hand, there are also plenty of compilers that do support
some C99 features, and declarations in for statements are probably
one of the most common implemented features (behind // comments,
I'd guess).

You'll need to decide for yourself whether using C99-specific
features is worth the (perhaps small) loss of portability.
The situation is not as simple as jacob claims it is -- or as both
jacob and I wish it were.
 
K

Kenny McCormack

Huh? What C do you use?

Not only can you, its recommended.

In the religion of comp.lang.c, nothing exists past C89.

Therefore, it (the declarations which are the subject of this thread)
does not exist.
 
L

lovecreatesbeauty

There are plenty of compilers that don't implement, or don't fully
implement, the C99 standard.

People are busy making new C standard while the old one arent' fully
implemented. What will the new one do - withdraw some odd features in
the old one?
 
N

Nick Keighley

The answers depend on what version of C you are using.

ie. if you the C you are using complies with 1989/90 standard (so-
called C89) or the 1999 standard (so-called C99). C99 supports A).
Noboddy supports option B)!
Also some
people use C++ as what is to all intents and purposes a C compiler
with a few differences,

I'm not sure what this is supposed to mean. C99 and C++ have identical
behaviour as regards defining variables in for loops.
one being that C++ always allows variables to
be declared anywhere.

well, no. C++ doesn't support option B) either.

To the OP use Richard Heathfield's reply rather than Malcolm's.
 
N

Nick Keighley

vaysagekv wrote:
A)Is it possible to declare a variable inside for loop like
   for(int i=100;i>=0;i--); [...]
if yes what is the scope of that variable?

According to the standard (both C99 and C++) the scope is restricted to
the loop, i.e. variable »i« is not visible outside the loop. However,
some older C++ compilers did this wrong. Some contemporary compilers
provide an option to switch scoping to the non-standard behaviour.

you sure they were actually wrong? I thought the definition of C++
changed. C++ wasn't actually standardised at the time, but was defined
by Stroustrup's book.
 
S

santosh

People are busy making new C standard while the old one arent'
fully implemented. What will the new one do - withdraw some odd
features in the old one?

As I understand from previous discussions here and in c.s.c, a
feature has to first be deprecated, before it can be removed.

AFAIK, gets will no longer be present in the next C standard, but
I've no idea about other withdrawals, if any.

What's interesting is that it's going to have support for concurrent
programming. For more discussions, better post in c.s.c.
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kenny said:
In the religion of comp.lang.c, nothing exists past C89.

Therefore, it (the declarations which are the subject of this thread)
does not exist.

C99 is not C?! READ THE STANDARD!
According to the standard:

This second edition cancels and replaces the first edition, ISO/IEC
9899:1990, as
amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/AMD1:1995, and
ISO/IEC 9899/COR2:1996. Major changes from the previous edition include:

That is, C99 is the ONLY standard NOW. The previous version has been
replaced by this version. If a C compiler does not fully implement this
standard, then it is not a fully-compliant C compiler.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkuE4l0ACgkQm4klUUKw07DVuQCfVcWTj8FIjvnwvDuWk3TzGNR7
kVAAn3fRpg7e23W0Jm7Ub7abiRkFXaxD
=ctIT
-----END PGP SIGNATURE-----
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top