casts to (void)

M.J.Shannon mjs at eagle.UUCP
Sat Jul 20 13:45:38 AEST 1985


> 
>     Why should a cast to (void) be necessary in a statement like
> 
> 	(void) printf("foo\n");
> 
> ?  Why not say that the expression in the definition
> <statement> = <expression>";" is automatically cast to type void?
> 
> 			Gary Ansok

The cast is necessary because printf() returns a value.  Lint (very properly)
warns that the value is unused without the cast.  Would you prefer that lint
say nothing if you had a code fragment something like:

	...
	log(d);
	...

Insofar as the language is concerned, failing to do *something* with a returned
value is at least suspect.  Casting the value to void *does* use the value, but
should generate no extra code; in some implementations, it could allow better
code generation because the compiler is told explicitly to ignore the value,
perhaps leaving more (or different) registers available for subsequent code.
-- 
	Marty Shannon
UUCP:	ihnp4!eagle!mjs
Phone:	+1 201 522 6063



More information about the Comp.lang.c mailing list