www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/05/03/04:38:15

Date: Wed, 3 May 1995 09:22:32 +0300
From: eliz AT is DOT elta DOT co DOT il (Eli Zaretskii)
To: A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk
Subject: Re: Bad bug with for(-;-;-) and an enum name
Cc: DJGPP AT SUN DOT SOE DOT CLARKSON DOT EDU

> #include<stdio.h>
> enum{q=1,imax=256};
> main(){double x,y; int i,j,k,l,im=imax;
> printf("A: "); for(i=-im  ;i<im  ;i++) printf("%d ",i); printf("\n");
> printf("B: "); for(i=-imax;i<imax;i++) printf("%d ",i); printf("\n");}
>
>   When this Gnu C++ program runs, loop A prints consecutive numbers on screen
> as expected, but loop B prints <<no numbers at all>>.

You are getting punished for grossly misusing (IMHO) the enum facility.
This facility exists with the sole purpose of declaring variables which
can accept only a certain number of discrete values.  In your case, you
told the compiler *explicitly* that the *only* value it can accept is
256.  So don't be mad when the compiler finds it appropriate to
implement imax as an unsigned int (or unsigned short), which means that
-imax isn't less than imax, and the loop never executes.  If you still
want to write your program with enums, you should explicitly cast it to
int *before* negating.

Did you try to compile with -Wall?  At least with non-enum variables it
warns you about comparisons which are always true/false.  I don't know
if it would do the same with enums, but I'd expect it to.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019