Mail Archives: djgpp/1995/05/03/04:38:15
> #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 -