Discussion in 'C Programming' started by BartC, Feb 14, 2013.

  1. annoyingly I sometimes see code that returns a bool result for such a
    convention. (I'm not sure if MS do this though)

    ah yes this is what I was thinking:-

    BOOL WINAPI DeleteFile(
    _In_ LPCTSTR lpFileName

    Return value

    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero (0). To get extended
    error information, call GetLastError.

    A sane convention I just wish they hadn't called it BOOL!

    Nick Keighley, Mar 9, 2013
    1. Advertisements

  2. scheme (which is sort of lispy) uses special values #t and #f.
    Anything that isn't #f is true.
    Nick Keighley, Mar 9, 2013
    1. Advertisements

  3. I quite like it. It seems to be used very consistently- which helps. I
    must admit in my own APIs I use Retval as the return type and it
    returns either OK (usually zero though I don't rely on it) or an error
    seriously? Why do you think MS go to the troble of returning an error?
    I wrapper every windows call

    if (WindowCall (hUngArian_t dodah) = -1)
    window_error ("WindowCall");

    and window_error does the GetLastError and translates it into a string
    and then throws (that's a C++ error handling thingy)
    nuffink usually. Occaisionally there is some recovery strategy for
    particular errors.
    Nick Keighley, Mar 9, 2013
  4. BartC

    BartC Guest

    If 'true' relates to success, and 'false' to failure, then that's the
    opposite of what is proposed in this thread by those talking about multiple
    failure codes.

    C also uses multiple 'true' values.
    BartC, Mar 9, 2013
  5. BartC

    BartC Guest

    Sometimes the specific error is only of interest at a certain level.
    Which is pretty much what I suggest later:
    BartC, Mar 9, 2013
  6. That's the point: true and false are *not* the same as success and
    failure, respectively.

    For a function that returns zero for success and non-zero for
    failure, the question being answered is not "Did this succeed?";
    it's "What went wrong?", with 0 as a special case meaning "Nothing".

    The way to express "Did this call to func() succeed?" is not
    `func()`, but `func() == 0`.

    I understand and agree that it would be more intuitive if it were
    the other way around. The convention is the way it is simply because
    it's more useful to have multiple error codes than to have multiple
    success codes.

    In a language that doesn't conflate numbers and booleans the way C does,
    this confusion doesn't arise. But once you accept the convention for
    what it is, it's not that hard to deal with.
    Keith Thompson, Mar 10, 2013
  7. BartC

    Phil Carmody Guest

    .... who were talking about C.

    C isn't LISP.

    I am not able rightly to apprehend the kind of confusion of
    ideas that could provoke such a conflation

    Phil Carmody, Mar 14, 2013
  8. Well, there is some similarity. Both C and Scheme have a
    more-or-less unique "false" value: #f for Scheme, and 0 for C
    (different kinds of zeros for different types, and only for scalars,
    but ...), with all other values being considered "true".

    This is in contrast to languages (like Pascal and Ada, for example) in
    which conditions can only be of a unique Boolean type with exactly two
    distinct values.

    The confusion, I think, is caused by the conflation of truth and
    falsehood with success and failure, respectively. That's certainly
    misleading in C, which has a strong convention of functions
    returning 0 for success and non-0 for failure. It's probably equally
    misleading in Scheme and other Lisps.
    Keith Thompson, Mar 14, 2013
  9. BartC

    Shao Miller Guest

    Agreed. As someone already mentioned, it depends on the question.

    void * ptr = malloc(12);
    /* Did malloc yield an invalid pointer? */
    if (!ptr)
    /* ... */

    "fail" and "succeed" are shortcuts that seem subjective. 'malloc' might
    fail to yield a null pointer value or succeed to give one. :)
    Shao Miller, Mar 14, 2013
  10. BartC

    Phil Carmody Guest

    Very true. But I do consider LISP to be a language far less oriented towards
    returning status values. Then again, I always programmed it as a fairly pure
    funtional language. C's far more side-effect oriented, and needs those
    status values everywhere, it never just returns the acted-upon thing in the
    same way LISP code so often does.

    Phil Carmody, Mar 15, 2013
  11. Phil Carmodyæ–¼ 2013å¹´3月15日星期五UTC+8上åˆ10時56分47秒寫é“:LISP is a small language that was designed to be used with slow jumps
    and fast stack operations in 195X-196X of the HW at that era.

    With the advance of technologies of mass-productions of mico-processors
    and so many teams could work out CPUs with different instruction sets,
    then that was the reason to call for a portable assembly language,
    i.e. c, in the 197X-199x.
    88888 Dihedral, Mar 15, 2013
  12. BartC

    Ian Collins Guest

    damercer2850 wrote:


    These random giganews posts appear to be pooing up all over Usenet.
    Ian Collins, Feb 3, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.