Why use (void) func() ?

Adam Stoller ghoti+ at andrew.cmu.edu
Tue Sep 25 07:48:57 AEST 1990


Excerpts from netnews.comp.lang.c: 21-Sep-90 Why use (void) func() ?
Dave Lee at dptechno.uucp (1070)

> Given a simple function like :

> int func(){ ... }

> Whose return value I rarely care about --- say printf().
> Why should I go to the extra trouble to write
> 	(void) printf("hello world\n");
[.....]
> If I wanted to check the return value, I would have.
> This is not the sort of thing that results from a typo or unconcious omission.
> I never type
> 	func_call();
> 	
> When I mean
> 	if( func_call() == whatever ) ...;

> I never "accidently" ommit a check for return value, though I may be
> lazy and decide to omit one.  I believe this is a programming
> consideration, not a language one.

a) You make the traditional assumption that just because YOU never do
anything wrong (exagerated emphasis) that NOBODY would ever do something
wrong.  It's an imperfect world, and mistakes happen.

b) Most of the cases of such functions are probably throwbacks to olden
days when:
    -) they didn't have void.
    -) they almost never bothered to declare anything that was, or
returned int.
    -) they may have actually had a real reason for sending the return value.

I happen to agree that having printf() return an int is rather a waste,
but if/when it bothers me, I either go through the code and put the void
cast in front of each printf, or I change them all from "printf" to
"PRINTF" and declare a macro like:

    #define PRINTF (void)printf

It's not beautiful, but at least it shows anyone who looks at my code
(including myself some days down the road) that I explicitly decided to
ignore the return value.

But then we breach into the holliest of holy wars --
 (oh noooooo! :0 oh yes! ;-)) 
-- styles and code formatting.

And so, adding my $0.02 to the pot, I return to the murky depths of my
own subconcious............


--fish



More information about the Comp.lang.c mailing list