expr?(void):(void)

Sam Kendall sam at think.COM
Thu Jul 31 12:01:26 AEST 1986


In article <1327 at ncoast.UUCP> allbery at ncoast.UUCP (Brandon Allbery) writes:
> > [Example in which expr ? f() : g() is used, where f and g return void.]
> 
> That's not a bug, it's a feature.  Literally.
> 
> Before you start complaining, consider that the intent of functions returning
> (void) is that of:
> 
> #define procedure void
> 
> procedure f1(x, y) {
> ...
> }

This is misleading.  Pascal and allied languages have a more
restrictive philosphy than C does; yet you are implying that because
procedures are allowed only as statements in Pascal, void-valued
expressions should be that way in C.  void expressions are always
allowed as the left operand of the comma operator, and as the first and
third expressions in the for statement; should be ban them there, too?
More graphically, should we cut off the hands of burglars in the U.S.
because it's done that way in Iran?

Enough cheap shots.  Here is an example of using void in "?:".  Suppose
you are writing a stdio-like package, and one function has this
specification:

	void PutC(Stream, char);

But for efficiency you want to write PutC as a macro.  So you define it
like this:

extern void _WriteBuf(Stream);
#define PutC(s, c)  (--(s)->count >= 0 ? (void) (*(s)->bufp++ = (c)) \
				       : _WriteBuf(s, c))

One can also easily come up with examples where void is useful as the
right operand of the comma operator, another place where it is illegal
in many compilers.

As far as I can figure, void is only useful in these contexts within
macros, and the reason it is useful there is that you want to make
macros syntactically expressions whenever possible, so you need to use
whatever control flow you can within expressions.

All in all, the ANSI committee did right in allowing void in these
places!

---
Sam Kendall			sam at godot.think.com
Thinking Machines Corp.		ihnp4!think!sam



More information about the Comp.lang.c mailing list