> I'm reading that C is generally NOT considered a block-structured
> language. Why not? I thought that the C language allows new name scopes
> to be introduced...C newbie... Thanks for your suggestions/opinions...
<Jack>
The only reason that I can think of that C would be considered less of a block
structured language than Pascal, for example, is that C does not provide
nested functions. That is you can't define a function inside another function
(except in the non-conforming mode of certain Gnu compilers which shall remain
nameless).
All function definitions in C are at the same level, and the only mechanism to
control linkage is the static keyword which limits linkage to a single source
file.
If there are other reasons I am sure we will here about them.
</Jack>
>> I'm reading that C is generally NOT considered a block-structured
>> language. Why not? I thought that the C language allows new name scopes
>> to be introduced...C newbie... Thanks for your suggestions/opinions...
> The only reason that I can think of that C would be considered less of a block
> structured language than Pascal, for example, is that C does not provide
> nested functions. That is you can't define a function inside another function
> (except in the non-conforming mode of certain Gnu compilers which shall remain
> nameless).
> All function definitions in C are at the same level, and the only mechanism to
> control linkage is the static keyword which limits linkage to a single source
> file.
IIRC, as ``blocky'' as Pascal (or at least the Borland/Inprise implementaton)
may be in terms of nested functions and procedures, unlike C, it doesn't
allow variables to be declared within the scope of a non-top-level block.
(One annoying quirk of the language, or at least Borland's implementation, is
that the counter variable in a FOR loop has an indeterminate value once the
loop terminates, in spite of the non-locality of its declaration).
--
========================================================================
Bob Nelson -- Dallas, Texas, USA (bne...@iname.com)
http://www.oldradio.com/archives/nelson/open-computing.html
``Those who don't understand UNIX are condemned to reinvent it, poorly.''
Where?
# Why not? I thought that the C language allows new name scopes
# to be introduced...C newbie... Thanks for your suggestions/opinions...
Variables and typedef names obey file scope when they are defined
outside of any function, otherwise they have block scope. Unlike
Pascal however, functions can't be defined inside other functions,
so in a sense, functions do not have block scope. But that's about
the only "non-blockscopy" issue I can think of in the C grammar
(macros are not part of the phrase structure grammar).
It probably depends on your definition of "block structure" whether
you regard C as block structured or not.
Regards,
Jens
--
Jens Schweikhardt http://www.shuttle.de/schweikh/
SIGSIG -- signature too long (core dumped)
Can this be true?
I was always taught that it was:
init; test; statement(s);
update; test; statement(s);
update; test; statement(s);
...
Therefore, you can rely on:
The loop never entering the first time if the test fails,
The update never executing if the loop never enters,
The variables remaining intact after the update and the test fails,
etc...
I've got lots of code that relies on these assumptions!
Whats the standard say?
-Scotty
In article <36865D...@flash.net> Spaceman Spiff <csw...@flash.net> wrote:
>Can this be true? ... Whats the standard say?
Presumably, Mr. Nelson was talking about Pascal above; and it is indeed
true. The idea is that:
for i := a to b do begin ... end;
can be compiled into "internal" machine code of the form:
if (b >= a) {
nloops = (b - a) + 1;
/* i = a; -- if needed */
do {
...
/* i++; -- if needed */
} while (--nloops);
}
which never updates "i" at all, if it is not used in the loop, and
which never even *sets* "i" (even if it *is* used) if b < a.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA Domain: to...@bsdi.com +1 510 234 3167
http://claw.bsdi.com/torek/ (not always up) I report spam to abuse@.
>Bob Nelson wrote:
>>
>> (One annoying quirk of the language, or at least Borland's implementation, is
>> that the counter variable in a FOR loop has an indeterminate value once the
>> loop terminates, in spite of the non-locality of its declaration).
I assume this is referring to Pascal.
>Can this be true?
>
>I was always taught that it was:
>init; test; statement(s);
>update; test; statement(s);
>update; test; statement(s);
>...
>Therefore, you can rely on:
> The loop never entering the first time if the test fails,
> The update never executing if the loop never enters,
> The variables remaining intact after the update and the test fails,
> etc...
>I've got lots of code that relies on these assumptions!
In C that's fine.
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------
> Bob Nelson wrote:
> >
> > (One annoying quirk of the language, or at least Borland's implementation, is
> > that the counter variable in a FOR loop has an indeterminate value once the
> > loop terminates, in spite of the non-locality of its declaration).
>
> Can this be true?
>
> I was always taught that it was:
> init; test; statement(s);
> update; test; statement(s);
> update; test; statement(s);
> ...
> Therefore, you can rely on:
> The loop never entering the first time if the test fails,
> The update never executing if the loop never enters,
> The variables remaining intact after the update and the test fails,
> etc...
> I've got lots of code that relies on these assumptions!
>
> Whats the standard say?
>
> -Scotty
<Jack>
I only used Pascal as an example because it is one of the three block
structured languages (other than C and C++) that I have some experience with.
The other two are PL/I and PL/M, and I remember even less about them than
about Pascal.
I also have never read the ISO standard for Pascal, but I do remember
important differences between its treatment of loop counters and other
languages, specifically that it is illegal to modify the value of the loop
counter inside the loop.
And AFAIR, the value of the loop counter is undefined after the loop ends.
</Jack>
Well, I was referring to C anyway. Sorry I didn't make that clear.
-Scotty
> >Can this be true? [SNIP]
> >I've got lots of code that relies on these assumptions!
>
> In C that's fine.
WHEW!!!!!
-Scotty
> Jack Klein wrote:
> >
> > On Sun, 27 Dec 1998 16:16:56 GMT, Spaceman Spiff <csw...@flash.net> wrote:
> >
> > > Bob Nelson wrote:
> > > >
> > > > (One annoying quirk of the language, or at least Borland's implementation, is
> > > > that the counter variable in a FOR loop has an indeterminate value once the
> > > > loop terminates, in spite of the non-locality of its declaration).
> > >
> > > Can this be true?
> > >
> > > I was always taught that it was: < Yada, Yada, Yada... >
> >
> > <Jack>
> > I also have never read the ISO standard for Pascal...
> > </Jack>
>
> Well, I was referring to C anyway. Sorry I didn't make that clear.
>
> -Scotty
<Jack>
In C the value of the counter is perfectly defined when the loop ends
to still contain exactly the same value it was last set to inside the
loop.
It is certain other Klewless(tm) languages which make loop counters
"special".
</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.
for i:= 1 to 10 do
begin
{do something and you can change counter value.}
end;
writeln("i=",i); { i = 10 }
ali murat
In article <369e7a8b....@netnews.worldnet.att.net>,
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
> I don't want to join discussion of which language is more structured but, in
> Turbo Pascal, counter value is still available after loop terminates, and you
> can change counter value in the loop.
>
> for i:= 1 to 10 do
> begin
> {do something and you can change counter value.}
> end;
>
> writeln("i=",i); { i = 10 }
>
> ali murat
<Jack>
Neither do I, and I could be wrong about my assertion. As I said, I
haven't used Pascal in a LOOOOOOONG time (I don't much like it), but I
remember that there was at least one mainstream language that I
haven't used in a LOOOOOOONG time where the value of the loop counter
is undefined once the loop ends.
I think its standard Pascal but, gosh, it could be Fortran 66! :)