Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

block-structured language...

1 view
Skip to first unread message

Luigi Scalabroni

unread,
Dec 26, 1998, 3:00:00 AM12/26/98
to
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 Klein

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
On Sat, 26 Dec 1998 22:58:03 -0800, Luigi Scalabroni <esca...@ix.netcom.com>
wrote:

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


Bob Nelson

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
Jack Klein <jack...@att.net> wrote:
> On Sat, 26 Dec 1998 22:58:03 -0800, Luigi Scalabroni <esca...@ix.netcom.com>
> wrote:

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

Jens Schweikhardt

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
Luigi Scalabroni <esca...@ix.netcom.com> wrote:
# I'm reading that C is generally NOT considered a block-structured
# language.

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)

Spaceman Spiff

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
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

Chris Torek

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
In article <764rr9$i4e$1...@renpen.nelson.org> Bob Nelson wrote:
[There was a paragraph comparing C and Pascal here, which csw...@flash.net
removed, then:]

>>(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).

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

Lawrence Kirby

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
In article <36865D...@flash.net> csw...@flash.net "Spaceman Spiff" writes:

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


Jack Klein

unread,
Dec 27, 1998, 3:00:00 AM12/27/98
to
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:
> 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>


Spaceman Spiff

unread,
Dec 28, 1998, 3:00:00 AM12/28/98
to
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

Spaceman Spiff

unread,
Dec 28, 1998, 3:00:00 AM12/28/98
to
Lawrence Kirby wrote:
>
> In article <36865D...@flash.net> csw...@flash.net "Spaceman Spiff" writes:
>
> >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? [SNIP]


> >I've got lots of code that relies on these assumptions!
>

> In C that's fine.

WHEW!!!!!

-Scotty

Jack Klein

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
On Mon, 28 Dec 1998 22:08:48 GMT, Spaceman Spiff <csw...@flash.net>
wrote:

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


alim...@my-dejanews.com

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
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

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

Jack Klein

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
On Tue, 29 Dec 1998 14:34:04 GMT, alim...@my-dejanews.com wrote:

> 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! :)

0 new messages