By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

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

P: n/a
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,
David.
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a


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.


Nov 14 '05 #2

P: n/a
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 don't believe it accomplishes anything like that. The compiler
probably can't do anything useful with the information that the return
value is unused - the code for printf() (which probably resides in some
library somewhere that the compiler doesn't know or care about) is
already returning a value. printf isn't going to be re-written to omit
the return just because you cast it to void.

To the best of my knowledge, the cast-to-void technique is used solely
to silence warnings from static code checking tools like LINT, or from
over-zealous compilers.
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 don't believe it is, or has ever been, an optimization, useful or
otherwise.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #3

P: n/a
"David M. Wilson" wrote:
.... snip ...
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.


Opinions vary, but one attitude is that:

a) This avoids generating 'result discarded' messages in lint.
b) This indicates to any future maintainer that there IS a
result being returned and discarded, which he may use if the
occasion arises.

Another attitude is that it is meaningless clutter.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4

P: n/a

On Tue, 6 Jan 2004, 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(...);


Hee hee!

Message-ID: <Pi**********************************@unix41.andre w.cmu.edu>
<quote>
Ben.c:17:7: Return value (type int) ignored: putchar(p[i])
Result returned by function call is not used.


Gosh. Lint really _is_ prehistoric, isn't it?


According to K&R2, putchar() does indeed return an int. I suspect
a cast to void would silence lint, but it's hardly worth it, is it?


Some old code, and some overly-portable code, does use casts to
void. We even get questions about (void)printf here occasionally.

</quote>

-Arthur
Nov 14 '05 #5

P: n/a
In article <99*************************@posting.google.com> ,
dw***********@botanicus.net (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.


If a function returns a value, and the caller doesn't use that return
value, then there are two possibilities:

1. The caller made a mistake and should have used the value.
2. The caller knew what he/she was doing and had no need for the
value.

When you cast the return value to void, you explicitely tell the
compiler that yes, you know that the function returns a value, and no,
you don't want to use it. Without that cast, many compilers will give a
warning telling you that you might have missed something.

It is also a good hint to other programmers.
Nov 14 '05 #6

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix45. andrew.cmu.edu>...
On Tue, 6 Jan 2004, 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(...);
Hee hee!

Message-ID: <Pi**********************************@unix41.andre w.cmu.edu>
<quote>
>Ben.c:17:7: Return value (type int) ignored: putchar(p[i])
> Result returned by function call is not used.

Gosh. Lint really _is_ prehistoric, isn't it?


According to K&R2, putchar() does indeed return an int. I suspect
a cast to void would silence lint, but it's hardly worth it, is it?


Some old code, and some overly-portable code, does use casts to
void.


To my knowledge, I've never read anything to suggest it was being done
for portability reasons. Is there any evidence of such?

I believe the standard inherited (and it remains in C99) the K&R C
behaviour of allowing functions which don't have an explicit return
statement to function as normal, so long as the calling function
didn't attempt to use a value from that function. So, C has always had
issues with ignored function return values.

I can understand why lint (et al) would give the warning, e.g...

sin(x);

But, I can't see that actual programming instances where the warning
might actually highlight an error would be terribly common.

I'm all ears (well eyes), if anyone has any anecdotes.
We even get questions about (void)printf here occasionally.

</quote>


--
Peter
Nov 14 '05 #7

P: n/a
[..]
I can understand why lint (et al) would give the warning, e.g...


What does "et al" mean?

--
Vijay Kumar R Zanvar
Nov 14 '05 #8

P: n/a
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes:
[..]
I can understand why lint (et al) would give the warning, e.g...


What does "et al" mean?


"and others"
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.