[Info-vax] decrementing & for loops in C

Bill Gunshannon billg999 at cs.uofs.edu
Thu Jan 8 09:27:49 EST 2009


In article <09411eb9-a739-4e87-9278-72b90a4143ce at k1g2000prb.googlegroups.com>,
	johnwallace4 at yahoo.co.uk writes:
> On Jan 8, 1:17 pm, "Bob Eager" <rd... at spamcop.net> wrote:
>> On Thu, 8 Jan 2009 13:03:32 UTC, vax... at chemie.uni-konstanz.de wrote:
>> > int main (void) {
>>
>> > int i,sum=0,n=10;
>>
>> >     (void)printf ("Starting countdown loop...\n");
>> >     (void)fflush (stdout);
>> >     for (i = n; i = 0; i--) {
>> >         (void)printf (" i: %i\n",i);
>> >     }
>> >     (void)printf ("done.\n");
>>
>> >     (void)printf ("Starting normal loop...\n");
>> >     (void)fflush (stdout);
>> >     for (i = 1; i <= n; i++) {
>> >         (void)printf (" i: %i\n",i);
>> >     }
>> >     (void)printf ("done.\n");
>> > }
>>
>> > Here is the output:
>>
>> > r SYSWRITE_TST
>> > Starting countdown loop...
>> > done.
>> > Starting normal loop...
>> >  i: 1
>> >  i: 2
>> >  i: 3
>> >  i: 4
>> >  i: 5
>> >  i: 6
>> >  i: 7
>> >  i: 8
>> >  i: 9
>> >  i: 10
>> > done.
>>
>> > So the countdown loop is ignored. g++ under linux behaves the same.
>>
>> There's a logical error in the code. The second part of if() is the
>> condition to be fulfilled to keep the loop going (see the normal loop,
>> where it's going to keep going as long as i is less than or equal to n).
>>
>> So it says:
>>
>>  for (i = n; i = 0; i--) {
>>
>> (i.e. "keep going as long as i is zero", which it obviously isn't)
>>
>> It should be:
>>
>>  f
>>
>> (i.e. "keep going as long as i is not zero")
>> --
>> Bob Eager
> 
> Nothing VMS specific here, just a well known problem with C and its
> users (including me).

It may be a problem with some C users, but it is definitely not a problem
with C.  It is doing exactly what it was told to do.  The users inability
to understand the semantics of the C language is not a shortcoing in C.

> 
> Some people suggest reversing the logical expression so the constant
> is on the left
> for (i = n; 0 != i; i--) { // Does what the user wanted
> 
> }
> By doing this, it is immediately obvious to the compiler that an
> assignment has been unintentionally used:
> for (i = n; 0 = i; i--) { // syntax error
> }

True, in this case, but in many cases it would not find the error as it
is a logical error and not syntactic.

> 
> Some kinder compilers will (optionally) offer a warning when an
> assignment rather than a logical expression is used in this context.

And, they would be wrong as there is nothing wrong with the statement
and determining logic is not a compilers job.

> 
> Occasionally (just occasionally) it's desirable to have an assignment
> rather than a logical expression. Allegedly.

Umm....  Somehow I doubt it.  And really good compiler would optimize
the loop away completely as it would always evaluate to true and therefore
never execute.

bill

-- 
Bill Gunshannon          |  de-moc-ra-cy (di mok' ra see) n.  Three wolves
billg999 at cs.scranton.edu |  and a sheep voting on what's for dinner.
University of Scranton   |
Scranton, Pennsylvania   |         #include <std.disclaimer.h>   



More information about the Info-vax mailing list