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


Groups > comp.lang.c > #99139

Re: scanf design

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: scanf design
Date 2016-12-12 14:02 -0800
Organization None to speak of
Message-ID <lnvauoq00p.fsf@kst-u.example.com> (permalink)
References <o2kh5p$e5q$1@dont-email.me> <96650663-827e-4f89-b78f-7b42a2516ef3@googlegroups.com> <o2mmfs$oag$1@dont-email.me> <lnpokxq7qq.fsf@kst-u.example.com> <36bfcef6-0961-4d95-9da0-0748e82831c8@googlegroups.com>

Show all headers | View raw


Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Monday, December 12, 2016 at 7:14:35 PM UTC, Keith Thompson wrote:
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>> Malcolm
>> >> But scanf is a bit unusual in that it returns a flexible number of
>> >> values of flexible type. So you see the & operator used heavily.
>> >> It's relatively rare to see the & operator used in most code.
>> 
>> I don't know how rare it is, but using the & operator certainly isn't
>> unusual.  For example, one common convention is for functions to return
>> an int with the value zero to indicate success, and non-zero to indicate
>> failure, with any other information being passed back via pointers.
>> 
> If you use that convention than, yes, unary & becomes common.
>
> But it's not very common to see that convention.

It certainly is in the programming I do.

>                                                  Partly because it
> is more natural to write
>
> if(getcheckstate(checkbox) == true)
>   makebackup();

Drop the "== true".

> rather than
>
> if(getcheckstate(checkbox, &checkflag) == ok)
> {
>     if(checkflag == true)
>       makebackup();
> }
> else
> {
>    /* we know the dialog is visible because of preceding code,
>       so what exactly is it supposed to mean for the checkbox
>       to have a non-valid checkstate? What do we do here? */
> }   

It means that there was a problem getting the state.  What we do here
depends on the application, but some kind of error handling is obviously
needed.  If you don't need a way to distinguish valid date from error
conditions, I suppose the simpler approach is ok.

> Another issue is that with the first snippet, all we need know
> is that getcheckstate returns some sort of integer. In
> the second snippet, checkflag might be a bool, or a bool_t, 
> or an XFlagType. And you've got to get it right.

Yes, you have to get the type right.  That's part of programming.

> I rarely use unary & in code written entirely by myself,
> though an exception is to return width and height for
> functions which create images of dimensions not controlled by
> caller.

In any case, the convention I mentioned (functions that return a status
indicator) was just one example of where unary & would commonly be used.
There are plenty of others.  I suspect your code is atypical.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

scanf design "Bill Cuningham" <nospam@nspam.invalid> - 2016-12-11 16:42 -0500
  Re: scanf design Noob <root@127.0.0.1> - 2016-12-11 22:55 +0100
    Re: scanf design "Bill Cuningham" <nospam@nspam.invalid> - 2016-12-11 17:03 -0500
  Re: scanf design Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 22:05 +0000
  Re: scanf design Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-11 14:43 -0800
    Re: scanf design "Bill Cunningham" <nospam@nspam.invalid> - 2016-12-12 12:25 -0500
      Re: scanf design Keith Thompson <kst-u@mib.org> - 2016-12-12 11:15 -0800
        Re: scanf design Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-12 13:17 -0800
          Re: scanf design Keith Thompson <kst-u@mib.org> - 2016-12-12 14:02 -0800
            Re: scanf design Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-12 15:06 -0800
              Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 15:58 -0800
                Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 16:10 -0800
                Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 16:10 -0800
                Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 16:22 -0800
                Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-15 07:30 -0800
                Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-15 07:56 -0800
                Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-15 08:21 -0800
    Re: scanf design "Bill Cunningham" <nospam@nspam.invalid> - 2016-12-12 13:18 -0500
      Re: scanf design Keith Thompson <kst-u@mib.org> - 2016-12-12 11:37 -0800

csiph-web