Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #76676

Re: While or if: function to get next operator or operand

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: While or if: function to get next operator or operand
Date 2015-11-20 16:14 -0500
Organization Self
Message-ID <564F8D3D.5020404@verizon.net> (permalink)
References <ca6f25b9-c067-4945-b4dd-2ba0a97ec136@googlegroups.com> <564F5520.8050503@verizon.net> <kfnsi409zwa.fsf@x-alumni2.alumni.caltech.edu> <lxL3y.112896$eE.77678@fx07.iad>

Show all headers | View raw


On 11/20/2015 03:38 PM, Lew Pitcher wrote:
> On Friday November 20 2015 15:31, in comp.lang.c, "Tim Rentsch"
> <txr@alumni.caltech.edu> wrote:
> 
>> James Kuyper <jameskuyper@verizon.net> writes:
>>
>>> On 11/20/2015 11:06 AM, Alla _ wrote:
>>>> Hello!
>>>>
>>>> Please, take a look at the function below.  It is from K&R.
>>>>
>>>> I dare to wonder why they use a while loop if the condition
>>>> considers only one item in the list. ...
>>>
>>> It does not.  The loop keeps executing until getch() returns
>>> something other than ' ' or '\t'.
>>>
>>>> ... Why not to use if statement?
>>>
>>> Because that would execute the getch() only once.
>>>
>>>> What is the purpose and the logic behind the while loop in this
>>>> case?
>>>>
>>>> I have accidentally found confirmation of my
>>>> concern on stackoverflow:
>>>>
>>>> QUOTE:
>>>> Lines like while ((s[0] = c = getch()) == ' ' || c == '\t'); make me wanna
>>>> weep. - ammoQ
>>>>
>>>> @ammoQ Indeed.  And then people still recommend K&R, I have
>>>> absolutely no idea why.  Look at this horrible code, it is not
>>>> written by the OP, it is written by K&R!  Had any rookie
>>>> programmer written code like this today, he would have been
>>>> immediately fired. - Lundin
>>>>
>>>> UNQUOTE
>>>>
>>>> Function:
>>>>
>>>> /* getop:  get next character or numeric operand */
>>>> int getop(char s[])
>>>> {
>>>>     int i, c;
>>>> /* the below while loop */
>>>>     while ((s[0] = c = getch()) == ' ' || c == '\t')
>>>>         ;
>>>
>>> I disagree with ammoQ.  Once you've learned enough about C, such
>>> code should be quite clear.  [...]
>>
>> For people who think this code snippet is clear - did you spot
>> the subtle bug?

Well, for one thing, the code should check whether c==EOF after exiting
from this loop, and take action accordingly.

> Perhaps not the subtle bug you allude to, but...
> 
> What is getch()?
> 
> IIRC, getch() is not part of the C standard library.

On many systems it's a function that works similarly to getchar(), but
takes it's input directly from the console. I haven't programmed for any
such system in two decades, but I am familiar with the fact that getch()
exists. I'd like to claim that this is the reason I didn't comment on
it, but the truth of the matter is that failed to notice that it wasn't
a call to getchar(). That's not a particularly subtle error, and it's
not a consequence of the complexity of this code, so it might not be
what he's referring to.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 08:06 -0800
  Re: While or if: function to get next operator or operand Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-20 08:17 -0800
    Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 16:44 +0000
  Re: While or if: function to get next operator or operand Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 11:23 -0500
    Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 08:39 -0800
      Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 16:47 +0000
      Re: While or if: function to get next operator or operand Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 11:54 -0500
        Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 09:13 -0800
        Re: While or if: function to get next operator or operand David Thompson <dave.thompson2@verizon.net> - 2015-12-06 08:04 -0500
      Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-20 12:02 -0500
  Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 16:41 +0000
    Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 09:11 -0800
      Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-20 17:39 +0000
        Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 10:42 -0800
        Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-21 14:58 -0500
          Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-23 12:21 +0000
        Re: While or if: function to get next operator or operand Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-21 21:29 +0000
          Re: While or if: function to get next operator or operand BartC <bc@freeuk.com> - 2015-11-21 22:31 +0000
            Re: While or if: function to get next operator or operand BartC <bc@freeuk.com> - 2015-11-22 10:57 +0000
              Re: While or if: function to get next operator or operand Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-22 13:45 +0000
          Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 01:31 -0500
          Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-23 04:33 -0800
            Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-23 23:44 -0800
        Re: While or if: function to get next operator or operand Rosario19 <Ros@invalid.invalid> - 2015-11-22 09:39 +0100
          Re: While or if: function to get next operator or operand Rosario19 <Ros@invalid.invalid> - 2015-11-22 09:49 +0100
            Re: While or if: function to get next operator or operand asetofsymbols@gmail.com - 2015-11-22 02:27 -0800
              Re: While or if: function to get next operator or operand asetofsymbols@gmail.com - 2015-11-22 02:32 -0800
        Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 10:02 -0500
          Re: While or if: function to get next operator or operand Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-23 17:23 +0000
            Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-23 12:31 -0500
              Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-23 13:02 -0500
        Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-22 07:06 -0800
          Re: While or if: function to get next operator or operand Barry Schwarz <schwarzb@dqel.com> - 2015-11-22 11:43 -0800
            Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-24 02:03 -0800
            Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-24 02:11 -0800
              Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-24 07:09 -0500
          Re: While or if: function to get next operator or operand Richard Heathfield <rjh@cpax.org.uk> - 2015-11-23 12:26 +0000
            Re: While or if: function to get next operator or operand raltbos@xs4all.nl (Richard Bos) - 2015-11-23 13:26 +0000
        Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-22 07:17 -0800
  Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-20 12:15 -0500
    Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 10:44 -0800
    Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 12:31 -0800
      Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-20 15:38 -0500
        Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-20 16:14 -0500
          Re: While or if: function to get next operator or operand Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-20 16:55 -0500
            Re: While or if: function to get next operator or operand Geoff <geoff@invalid.invalid> - 2015-11-20 15:04 -0800
        Re: While or if: function to get next operator or operand Barry Schwarz <schwarzb@dqel.com> - 2015-11-20 20:59 -0800
        Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 23:50 -0800
          Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-21 00:18 -0800
        Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 14:10 -0800
      Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-22 19:00 -0800
        Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-23 06:57 -0500
          Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-23 23:49 -0800
            Re: While or if: function to get next operator or operand James Kuyper <jameskuyper@verizon.net> - 2015-11-24 07:13 -0500
        Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-23 07:55 -0800
          Re: While or if: function to get next operator or operand Philip Lantz <prl@canterey.us> - 2015-11-23 23:36 -0800
            Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 13:32 -0800
    Re: While or if: function to get next operator or operand Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 16:13 -0500
      Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 13:54 -0800
  Re: While or if: function to get next operator or operand Geoff <geoff@invalid.invalid> - 2015-11-20 12:19 -0800
  Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 15:32 -0800
    Re: While or if: function to get next operator or operand Chad <cdalten@gmail.com> - 2015-11-20 18:38 -0800
      Re: While or if: function to get next operator or operand Keith Thompson <kst-u@mib.org> - 2015-11-20 19:45 -0800
        Re: While or if: function to get next operator or operand Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-20 20:12 -0800
      Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 20:45 -0800
        Re: While or if: function to get next operator or operand Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-20 21:04 -0800
      Re: While or if: function to get next operator or operand Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 13:57 -0800
    Re: While or if: function to get next operator or operand Alla _ <modelling.data@gmail.com> - 2015-11-20 20:43 -0800

csiph-web