[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

C99 question

Joe Wright

4/10/2011 10:00:00 PM

Given..

for (int i = 0; i < 10; ++i) {
...
}

...when the for loop exits, is i still visible? I'd say yes but a
professional instructor in things C* says i disappears at the ending '}'.

--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."
25 Answers

Ian Collins

4/10/2011 10:02:00 PM

0

On 04/11/11 09:59 AM, Joe Wright wrote:
> Given..
>
> for (int i = 0; i < 10; ++i) {
> ...
> }
>
> ...when the for loop exits, is i still visible? I'd say yes but a
> professional instructor in things C* says i disappears at the ending '}'.

It goes out of scope at the closing brace. Try it for your self:

for (int i = 0; i < 10; ++i) {}

int n = i;

--
Ian Collins

William Ahern

4/10/2011 10:11:00 PM

0

Joe Wright <joewwright@comcast.net> wrote:
> Given..

> for (int i = 0; i < 10; ++i) {
> ...
> }

> ..when the for loop exits, is i still visible? I'd say yes but a
> professional instructor in things C* says i disappears at the ending '}'.

Read section 6.8.5.3 of the standard and decide for yourself. The most
recent draft is at

http://www.open-std.org/jtc1/sc22/wg14/www/docs...

I took the link from

http://www.open-std.org/jtc1/sc22/wg14/www/standards...

Peter Nilsson

4/10/2011 10:15:00 PM

0

Ian Collins <ian-n...@hotmail.com> wrote:
> Joe Wright wrote:
> > Given..
> >
> > for (int i = 0; i < 10; ++i) {
> > ...
> > }
> >
> > ...when the for loop exits, is i still visible? I'd say yes
> > but a professional instructor in things C* says i disappears
> > at the ending '}'.
>
> It goes out of scope at the closing brace.

True.

> Try it for your self:
>
>    for (int i = 0; i < 10; ++i) {}
>    int n = i;

A compiler can claim conformance to C90 and allow such an
extension. Unfortunately, there are a few that do. More
unfortunately, they seem to be compilers of choice in many
educational institutions.

--
Peter

Tim Rentsch

4/11/2011 12:12:00 AM

0

Peter Nilsson <airia@acay.com.au> writes:

> Ian Collins <ian-n...@hotmail.com> wrote:
>> Joe Wright wrote:
>> > Given..
>> >
>> > for (int i = 0; i < 10; ++i) {
>> > ...
>> > }
>> >
>> > ...when the for loop exits, is i still visible? I'd say yes
>> > but a professional instructor in things C* says i disappears
>> > at the ending '}'.
>>
>> It goes out of scope at the closing brace.
>
> True.
>
>> Try it for your self:
>>
>> for (int i = 0; i < 10; ++i) {}
>> int n = i;
>
> A compiler can claim conformance to C90 and allow such an
> extension.

Not without issuing a diagnostic it can't.

Keith Thompson

4/11/2011 2:49:00 AM

0

Joe Wright <joewwright@comcast.net> writes:
> Given..
>
> for (int i = 0; i < 10; ++i) {
> ...
> }
>
> ..when the for loop exits, is i still visible? I'd say yes but a
> professional instructor in things C* says i disappears at the ending '}'.

As others have said, i goes out of scope at the closing brace.

When this feature was first introduced into C++, it was defined so
that the scope of i extended to the enclosing block, but this was
changed as of the 1998 ISO C++ standard. Some older C++ compilers,
and some pre-C99 C compilers that offer it at an extension, might
implement the older semantics. But you certainly shouldn't depend
on it.

Out of curiosity, what led you to think that i is still visible
after the end of the loop?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Eric Sosman

4/11/2011 11:10:00 AM

0

On 4/10/2011 5:59 PM, Joe Wright wrote:
> Given..
>
> for (int i = 0; i < 10; ++i) {
> ...
> }
>
> ..when the for loop exits, is i still visible? I'd say yes but a
> professional instructor in things C* says i disappears at the ending '}'.

The pro is right.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Malcolm McLean

4/11/2011 11:31:00 AM

0

On Apr 11, 5:49 am, Keith Thompson <ks...@mib.org> wrote:
>
> Out of curiosity, what led you to think that i is still visible
> after the end of the loop?
>
C allows this

for(i=0;i<N;i++)
if(somecondition(i))
break;
if(i == N)
goto condition_not_triggered;

however a lot of programming languages won't allow a similar
construct, because their counting variables are only visible within
the scope of the for loop.

This can lead to a certain amount of confusion.

ram

4/11/2011 12:42:00 PM

0

Keith Thompson <kst-u@mib.org> writes:
>Out of curiosity, what led you to think that i is still visible
>after the end of the loop?

It vaguely reminds me of Microsoft® Visual C++ 6.0, which does
not accept IIRC

#include <iostream>
#include <ostream>

int main()
{ int i = -22;
for( int i = 0; i < 10; ++i )std::cout << i << '\n';
std::cout << i << '\n'; }

Main.cpp(6) : error C2374: 'i' : redefinition; multiple initialization

. See also

http://support.microsoft.com...

. (The above, of course, is not a C program, but a C++ programm.
C++ is a programming language that is similar to C in some parts.)

And, of course, C is called »C« (not »C99«).

»This second edition cancels and replaces the first
edition, ISO/IEC 9899:1990«

ISO/IEC 9899:1999 (E)

However, as I wrote before, I actually hate it when a party
redefines the meaning of words within such a short period as
decades, because - for example - now, whenever you read a
text with »C«, you need to find out the exact date it was
written to known which language it refers to. I'd say:
Another language, another name.

ISO/IEC 9899:1999 (E) should at least have provided a long
unique name for the language it defines, such as »C99«. It
then might have added that this is abbreviated to »C in this
document« or »may also be called "C"«.

Tim Prince

4/11/2011 1:08:00 PM

0

On 4/11/2011 4:31 AM, Malcolm McLean wrote:
> On Apr 11, 5:49 am, Keith Thompson<ks...@mib.org> wrote:
>>
>> Out of curiosity, what led you to think that i is still visible
>> after the end of the loop?
>>
> C allows this
>
> for(i=0;i<N;i++)
> if(somecondition(i))
> break;
> if(i == N)
> goto condition_not_triggered;
>
> however a lot of programming languages won't allow a similar
> construct, because their counting variables are only visible within
> the scope of the for loop.
>
> This can lead to a certain amount of confusion.
>
Particularly in the style used by a customer of mine, where this
counting variable shadowed one in the outer scope.
We broke company over their refusal to change dependencies on
sizeof(size_t) == sizeof(int).

--
Tim Prince

Keith Thompson

4/11/2011 3:37:00 PM

0

Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Apr 11, 5:49 am, Keith Thompson <ks...@mib.org> wrote:
>>
>> Out of curiosity, what led you to think that i is still visible
>> after the end of the loop?
>>
> C allows this
>
> for(i=0;i<N;i++)
> if(somecondition(i))
> break;
> if(i == N)
> goto condition_not_triggered;
>
> however a lot of programming languages won't allow a similar
> construct, because their counting variables are only visible within
> the scope of the for loop.
>
> This can lead to a certain amount of confusion.

Thank you, but my question was directed to Joe Wright, the original
poster.

Note that in the above case, i must be declared prior to the for loop,
and its scope has nothing to do with the fact that it happens to be used
in a for loop. The issue was for loops which declare the variable.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"