Scope of a variable declared in for loop

G

gwowen

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.

Correct (though the "Stroustrop's book" in this context isn't D&E,
which is being discussed else-thread.) Standard C++ has always been
"scope is just the loop", but pre-standard C++ -- that described in
The C++ Programming Language 2nd Ed, it lasts till the end of the
inner block in which the for() lives (r6.5.3).

for(init;exp1;exp2)
statement;

used to be equivalent to

init;
while(exp1){
statement;
exp2;
}

Now its equivalent to

{
init;
while(exp1){
statement;
exp2;
}
}

Some compilers (e.g. g++ ) implement both with a switch to select
between them (-ffor-scope)
 
M

Mark Bluemel

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.

Thanks for the correction. I should have checked C99, but given its
absence from my day-to-day existence, I tend to forget about it.
 
K

Keith Thompson

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?

Chris Hills, who is a member of the committee, has stated here
that C1X is likely to drop a number of C99 features, for example
in Message-ID: <[email protected]>.
I've seen no confirmation of this from anyone
else, and the C201X committee drafts (the latest is
<http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1425.pdf>) have
shown no signs of such an effort; they're based on the C99 standard
with clarifications and new features (including threading).

Personally, I think dropping C99-specific features from C201X would
be a bad idea. Though C99 as a whole isn't as widely available
as I'd like, there are *some* conforming or nearly conforming
implementations, and there's code that depends on the new features.
Breaking existing code is not absolutely prohibited, but it's
something the committee has gone to great lengths to avoid.
 
K

Keith Thompson

Michael Tsang said:
Kenny McCormack wrote: [the usual]

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

Don't worry about what Kenny says. He's lying.
 
S

santosh

Michael Tsang said:
Kenny McCormack wrote: [ ... ]
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.

Strictly speaking, I guess you're right. But real world is "fuzzier."

A Standard's aim and reason for coming into existence is to get
widely, if not unanimously, implemented. Otherwise it can be regarded
as not having achieved it's purpose. Implementations always have the
final say, since it's they which ultimately get the job done, not the
standards.

Arguably, the large portion of the reason for why Standard C has not
been widely implemented has to be borne by the ISO committee, for
standardising features lacking broad consensus across atleast most
implementations out there. This was I guess the key difference from
the C90 standardisation.

Let's hope the upcoming standard meets with better success than
C99:)

And Kenny was being sarcastic there. He full well knows that C99 is
the current C Standard.
 
J

jacob navia

Keith Thompson a écrit :
Michael Tsang said:
Kenny McCormack wrote: [the usual]
C99 is not C?! READ THE STANDARD!
According to the standard:
[snip]

Don't worry about what Kenny says. He's lying.
Don't worry about Thomson, he is just obsolete.
He thinks anybody that doesn't agree with the C89 clique here
is lying...
 
J

jacob navia

Mark Bluemel a écrit :
Thanks for the correction. I should have checked C99, but given its
absence from my day-to-day existence, I tend to forget about it.

Please abstain from answering nonsense then. Check if you know the
answer, THEN answer.

Thanks in advance.
 
G

gwowen

I cant remember the time I last used a C compiler that didn't support
such declarations.

Green Hills compiler 424 for ARM9 does not support that extension.
TI's compiler for their Piccolo DSP does not support that extension.

I use both of these these frequently. These compilers are not for
outmoded valve powered mainframes -- ARM9's are pretty much the most
ubiquitous microprocessors in the world. You probably have one or two
about your immediate person.

Do you ever write code for non-desktop machines?
 
B

Ben Bacarisse

Nick Keighley said:
I'm not sure what this is supposed to mean. C99 and C++ have identical
behaviour as regards defining variables in for loops.


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

Sorry but it does. It's off topic so I won't give you all the
quotations but C++ permits a declaration in the condition part of an
'if', 'while', 'for' or 'switch' (but not 'do' of course).
 
J

jacob navia

gwowen a écrit :
Green Hills compiler 424 for ARM9 does not support that extension.
TI's compiler for their Piccolo DSP does not support that extension.

I use both of these these frequently. These compilers are not for
outmoded valve powered mainframes -- ARM9's are pretty much the most
ubiquitous microprocessors in the world. You probably have one or two
about your immediate person.

Do you ever write code for non-desktop machines?

You are talking nonse or just LYING

I cite from
http://www.ghs.com/news/20070131_compiler_version5.html
that is the official site of Green Hills Software:

Standards and Reliability

The Green Hills compiler was the first compiler for embedded systems to achieve 100% conformance to
ANSI/ISO standards for C and C++. In addition, the new compiler supports the latest C99
specification and the latest MISRA C standard. The Green Hills compilers are tested against industry
standard validation suites, including Plum Hall, and are also tested against the industry’s most
proven and extensive regression test suite.

That version was out in 2007!!!!

And now you are telling us that "there is no C99 support" in Green Hills
compilers?
 
J

jacob navia

gwowen a écrit :
TI's compiler for their Piccolo DSP does not support that extension.

The "Piccolo dsp" is not a stand alone processor and must be used with an
ARM7 processor, a product line introduced in 1992, 7 YEARS BEFORE the C
standard was out in 1999. We see here the bad faith of this people when
complaining about C99.
 
R

Rob Kendrick

gwowen a écrit :

The "Piccolo dsp" is not a stand alone processor and must be used
with an ARM7 processor, a product line introduced in 1992, 7 YEARS
BEFORE the C standard was out in 1999. We see here the bad faith of
this people when complaining about C99.

ARM7 was certainly 1992, but Piccolo? I don't think so. And what is
one meant to use to compile C for running on the DSP part? An ARM
compiler?

B.
 
K

Kenny McCormack

Michael Tsang said:
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.

ITA (Yes, as others have pointed out, my original post was somewhat
satirical [of the CLC regs])

But the point is that that (what you write above) is *not* the official
dogma of CLC. The official dogma of CLC, as enunciated by leader
Heathfield, is that nothing exists beyond C89. This is further
enunciated by, among others, subordinate Gowen (see his "Green Hills
supports nothing beyond C89" threads).

HTH
 
N

Nick Keighley

Sorry but it does.  It's off topic so I won't give you all the
quotations but C++ permits a declaration in the condition part of an
'if', 'while', 'for' or 'switch' (but not 'do' of course).

ah, thanks. How recent is that?
 
M

Malcolm McLean

The main point remains : its a nice construct and recommended. The more
local a variable declaration the better for all concerned, especially
when dealing with things like loop variables where the end of the loop
means the end of our interest in it.
The problem is that you run into another issue. There's a limit to the
number of levels a human being can keep in mind at any one time.
I think this is an example of the "rule of three" (three levels of
parentheses, three indirections, three dimensions), though I might be
persuaded that in fact it is the rule of five (five layers in a tree
or graph). Certainly you can't have arbitrary levels of scope.
We've already got global globals and file scope globals. Function
scope locals make three. I don't think we want any more.
 
S

santosh

Malcolm McLean said:
The problem is that you run into another issue. There's a limit to
the number of levels a human being can keep in mind at any one
time. I think this is an example of the "rule of three" (three
levels of parentheses, three indirections, three dimensions),
though I might be persuaded that in fact it is the rule of five
(five layers in a tree or graph). Certainly you can't have
arbitrary levels of scope. We've already got global globals and
file scope globals. Function scope locals make three. I don't think
we want any more.

Isn't it just a special case of block scope, as is function scope
itself?

I agree that it's a nice convenience, but it can be easily done
without. If functions aren't overly long, it's pretty simple to
declare and use a single, or few, counters throughout the function.
 
J

jacob navia

Richard Heathfield a écrit :
jacob said:
Keith Thompson a écrit :
Kenny McCormack wrote:
[the usual]
C99 is not C?! READ THE STANDARD!
According to the standard:
[snip]

Don't worry about what Kenny says. He's lying.
Don't worry about Thomson, he is just obsolete.
He thinks anybody that doesn't agree with the C89 clique here
is lying...

It would be lame to flame you for your spelling error, but it is common
courtesy to get people's names right even if the rest of your spelling
is shot all to pieces.

Well, I did not call him a jerk, as he did to me some time ago.
What a sin! Misspelling thompson's name...
To call Keith Thompson "obsolete" is to introduce a belligerent tone
into what ought to be a technical issue.

Yes. C89 is obsolete and Thompson with it.
I doubt whether *anyone* here disputes that C99 is the de jure Standard.
Anyone who pretends it's the de facto Standard needs to take a long hard
look out there in the Real World, where only a very small percentage of
C programmers actually use a 100% conforming C99 implementation.


There is only ONE fully conforming C++ 1998 implementation, 12 years
after that standard was printed.

So what?

C++98 is a "failed language"?

You and thompson go on diffusing misinformation. If you stay
with an obsolete standard you are obsolete. Period.
 
K

Keith Thompson

Richard Heathfield said:
jacob said:
Keith Thompson a écrit :
Kenny McCormack wrote:
[the usual]
C99 is not C?! READ THE STANDARD!
According to the standard:
[snip]

Don't worry about what Kenny says. He's lying.
Don't worry about Thomson, he is just obsolete.
He thinks anybody that doesn't agree with the C89 clique here
is lying...

It would be lame to flame you for your spelling error, but it is
common courtesy to get people's names right even if the rest of your
spelling is shot all to pieces.

To call Keith Thompson "obsolete" is to introduce a belligerent tone
into what ought to be a technical issue.

To be fair, I introduced a belligerent tone myself when I called Kenny
a liar.

Though this doesn't justify jacob's latest insult against me.

[...]
 
K

Keith Thompson

Nick Keighley said:
ah, thanks. How recent is that?

Stroustrup mentions declarations in conditions in the 1994 edition of
"The Design and Evolution of C++". It's probably also in the 2nd
edition of The C++ Programming Language, but I don't have my copy
handy.

ObC: C99 picked up declarations in for statements (with the better
scoping rule that C++ eventually adopted), but not declarations in
conditions.
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top