"for" loops (was Re: C++ vs. Modula2)
Roger Crew
crew at Polya.Stanford.EDU
Tue Feb 7 04:39:31 AEST 1989
In article <1004 at ubu.warwick.UUCP>, geoff at warwick (Geoff Rimmer) writes:
> In article <6419 at polya.Stanford.EDU> crew at Polya.Stanford.EDU (Roger Crew) writes:
> >Here's a quiz:
> >How would you write the following in C?
> >
> > VAR a, b, c : Char;
> > ...
> > FOR c = a TO b DO ... END;
> >
[ ... yup, this discussion ...]
>
> What a stupid question. If you are going to count from 0 to 255, you
> wouldn't use char in the first place - you'd use unsigned char, or
> int. I can't see what you're getting at, at all.
I'll grant that, for the benefit of compilers that *have* signed
chars, I actually did intend to say ``unsigned char'' rather than
``char''. The fact of the matter is, however, that
unsigned char a;
for( c=0; c<=255; ++c) {...}
still doesn't work. You can try it if you like, or you can read the
submissions from the other 10 posters who understand what's going on...
The fact that I was using chars signed or unsigned is irrelevant to
the point I was trying to make, namely that the semantics of C
for-loops and Modula/Pascal for-loops are entirely different -- it is
not simply a case of one being more general than the other. While
it's fairly obvious that a general C for-loop often can't be written
as a Modula for-loop, it's not so obvious (or at least, *I* didn't think
it was completely obvious...) that to do a general Modula for-loop
in C *also* requires strange contortions.
As for *why* I'd want to be counting with chars, just consider any
application requiring some kind of array indexed by chars, e.g.,
VAR font : ARRAY Char OF Bitmap;
It's an obvious thing to do in Modula. I suppose this also
illustrates the danger of carrying over one languages way of thinking
into another, a problem that does not confine itself to for-loops.
> Go back to writing Fortran. Let the *real* programmers use C. :-)
I'll let this statement speak for itself. :=)
--
Roger Crew ``Beam Wesley into the sun!''
Usenet: {arpa gateways, decwrl, uunet, rutgers}!polya.stanford.edu!crew
Internet: crew at polya.Stanford.EDU
More information about the Comp.lang.c
mailing list