Re: Void typecasting

Discussion in 'C Programming' started by Malcolm McLean, May 27, 2014.

  1. If a function returns a value, the idea is that you tell the compiler you are
    deliberately not using that value. But many standard library functions return
    values which are of limited use and are normally ignored, and the casts
    just clutter the code.
    Virtually nothing. There might be a few technicalities buried away in the
    standard about behaviour when functions return signalling NaNs and the like,
    but for all practical purposes you can ignore these.
    If you're hazy on that, just forget all about casting the results of unused
    functions, and clear up your understanding of pointers. That's essential to
    C, the other is just obscure detail which most C programmers don't know.
    Malcolm McLean, May 27, 2014
    1. Advertisements

  2. Which is why the above behavior is not only clutter, but even actively

    Deliberate discarding of values has its place, but should always be
    deliberate and not automatic. When a compiler has the ability to warn
    unused return values of functions (for example as gccs warn_usused
    attribute does), you'll inadvertendly suppress this warning if the
    return value is accidently unused.

    I don't know why some people still teach that explicit discarding is a
    good thing, but have seen quite a few people actually doing this.


    Ah, der neueste und bis heute genialste Streich unsere großen
    Kosmologen: Die Geheim-Vorhersage.
    - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$>
    Johannes Bauer, May 27, 2014
    1. Advertisements

  3. Malcolm McLean

    Stefan Ram Guest

    You already tell this to /the compiler/ by the statement


    . The »(void) « is for uneducated /human/ readers, not for
    the compiler.
    Stefan Ram, May 27, 2014
  4. Malcolm McLean

    Jorgen Grahn Guest

    And you normally don't use it on strcpy() and similar functions, but
    on functions where it makes sense to check the return value because
    they can signal failures. To me,


    says "Yes, I am aware that fclose() as a general rule can return an
    error code which should be dealt with, but in /this/ particular case I
    made a judgement call and chose to ignore that information. Dear
    reader, please don't assume you can do the same to every fclose()

    Jorgen Grahn, May 27, 2014
  5. The trouble with this reading is that it is not the only one. Sometimes
    people cut-and-paste code without thinking. Some people use (void)
    without any thought at all because it is mandated in certain cases by
    coding standard. Other might do it just to silence some warning from
    the compiler whist "hacking" the code and the forgot to come back and
    check the return. If you write

    fclose(input_file); // result need not be checked here


    fclose(input_file); // FIX ME: check the return before v1.0

    I think you are on safer ground. (Or course you can add a (void) cast
    as well, but I would not bother).
    Ben Bacarisse, May 27, 2014
  6. Malcolm McLean

    Jorgen Grahn Guest

    True; best case it really just tells the reader that /something/ went
    through my head, but not /what/. I guess it's a tradeoff: explain
    this choice in detail and you may have to explain a lot of other
    choices in detail too, or they will look less important in comparison.
    Me neither: if you have the comment, the cast adds no value.

    Jorgen Grahn, May 27, 2014
  7. Malcolm McLean

    Ian Collins Guest

    Some versions of lint will whinge if the unused return isn't explicitly
    discarded. Projects that use specific lint options have a rule to add
    the other pointless and annoying (void).
    Ian Collins, May 28, 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.