When does void make code less readable?

Bill Tuthill tut at sun.uucp
Sat Feb 16 05:50:07 AEST 1985


In general, void is A Good Thing.  C routines that return a value
are like Pascal functions, while C routines not returning values
are like Pascal procedures, and should be declared as void to keep
things clear.  However, some system/library routines, such as close(),
fclose(), and free() return values that programs don't usually care
about.  If the system is unable to close a file, you've got problems
that a normal program can't deal with anyway.  Nonetheless, lint
encourages you to cast the return value of these routines to void.
Now, I invite people to prove that the simple

	fclose(fp);

is less portable than:

	(void)fclose(fp);

I consider the first more portable-- many compilers on cheap micros
don't know about void.  The first version is also more readable--
anytime you throw extra keywords in, legibility decreases.

Bill Tuthill



More information about the Comp.lang.c mailing list