Mail Archives: djgpp/1998/09/09/15:39:58
On 9 Sep 1998, loki wrote:
> while (c=getkey() != 'q') {
> printf("%d\n",c); fflush(stdout);
> }
>
> Now, the strange thing is that all this prints out is 1's
You forgot about the operator precedence: != binds *before* =, so the
code generated by the above first calls `getkey', then compares the
return value to `q', and *then* assigns the result of this comparison (1
or 0, depending on what you have read) to `c'.
You should write it thusly:
while ((c=getkey()) != 'q') {
and then it will do what you want.
Btw, compiling with -Wall switch should cause the compiler to emit a
warning for that line. You should always use -Wall, IMHO.
- Raw text -