www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/09/15:39:58

Date: Wed, 9 Sep 1998 19:31:12 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: loki <loki AT nectar DOT com DOT au-remove>
cc: djgpp AT delorie DOT com
Subject: Re: problems reading from keyboard
In-Reply-To: <slrn6vd10f.4b.loki@anima.nectar.com.au>
Message-ID: <Pine.SUN.3.91.980909192724.2901M-100000@is>
MIME-Version: 1.0

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 -


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