Re: [Slightly OT] Casting non-void function results to void in modern compilers.

From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 01/06/04


Date: Tue, 06 Jan 2004 03:05:31 -0600


David M. Wilson wrote:

> I know this doesn't specifically pertain to the scope of comp.lang.c,
> but I don't know of a better forum to post it on, besides, what is the
> practicality of comp.lang.c if it weren't for answers to questions
> such as this? :)
>
> I see an awful lot of code on a day to day basis like so:
>
> (void) printf(...);
>
>
> I was told in my fledgeling years that this was used as a hint to the
> compiler to tell it not to save the return value of the called
> function, because it isn't used. I always wondered about that,
> considering the statement is not an assignment expression, and the
> return value 'flows off the end of the expression' anyway.
>
> Is this a useful modern optimisation? I would have thought not, but I
> still see it in modern code. Surely a modern (or even ancient)
> compiler can see that the result of printf() is not used, and so it
> should perform any possible optimisations based on the fact the return
> value isn't used.
>
> Am I wrong? Thanks,

The compiler can see that the return code isn't used, but it can't tell
whether or not it SHOULD have been used. By the author specifying the
"void" cast, they're telling the compiler (or lint) not to issue a
warning for that line as the return code is, in this case, being
intentionally discarded.

        Ed.

>
> David.



Relevant Pages

  • [Slightly OT] Casting non-void function results to void in modern compilers.
    ... considering the statement is not an assignment expression, ... Is this a useful modern optimisation? ... compiler can see that the result of printfis not used, ...
    (comp.lang.c)
  • Re: VC2005 Pro: IDE (Compiler ?) cant find Stdafx.h
    ... > David F wrote: ... > you told the compiler to include a file that does not exist. ... I also recalled that the "NOT using precompiled header files" was not by ... > emphasize SOURCE file - What else would a compiler be looking for? ...
    (microsoft.public.vc.language)
  • Re: What to get?
    ... I have to agree with David on this one. ... Main reason being on the "Base" side of a some older ... Accordions you can get chords of four and five notes being played ... compared to most modern ones only having three notes playing the chord ...
    (rec.music.makers.squeezebox)
  • Re: OpenVMS Management Station - HSZTERM
    ... Early HSZs and HSZ firmware did not provide a virtual console LUN; ... My initial attempts to unzip the file failed, ... You will need a C Compiler on ... My thanks to David for his help in getting this set up on my system. ...
    (comp.os.vms)
  • Re: KINGS
    ... Kings Saul and David and brings them into the modern era. ... symbolism of the butterflies? ...
    (soc.culture.jewish.moderated)