C SYNTAX QUESTION

Karl Swartz kls at ditka.Chicago.COM
Mon Feb 4 10:04:19 AEST 1991


In article <43415 at nigel.ee.udel.edu> HBO043%DJUKFA11.BITNET at cunyvm.cuny.edu (Christoph van Wuellen) writes:
>Is this correct, and if so, why?

(all but critical fragments deleted)

>void void_function();

>i ?  void_function() : 0;

>In GNU GAS, file obstack.h, many macros of such type are declared, and
>my c68/c386 compiler [K&R] complains about that..

>i ? void_function() : (void) 0;

>works.

For a strictly K&R compiler the question is irrelevant since there is
no void type in K&R.  If c68/c386 is a "K&R+" compiler, with a few
additions such as void, the rules are whatever the compiler writers
decided made sense to them.

For ANSI C, going by the old draft that I have at hand, the first
version of the code (without the cast to void on the third operand)
is not legal C.  The conditional operator is described in section
3.3.15, with the following constraints:

    The first operand shall have scalar type.

    One of the following shall hold for the second and third operands:

  * both operands have arithmetic type;

  * both operands have the same structure or union type;

  * both operands are pointers to the same type;

  * both operands are pointers to objects that have qualified or
    unqualified versions of the same type;

  * both operands have void type;

  * one operand is a pointer and the other is a null pointer constant;
    or

  * one operand is a pointer to an object or incomplete type and the
    other is a pointer to void.

One operand of type void and the other of type int don't meet these
constraints.

One compiler that I encountered (an older version of VAX-11 C for VMS)
didn't even allow a void *result* from a conditional expression, which
required a bit of hackery along the lines of:

    void void_function();

    (void) i ? (void_function(), 0) : 0;

Blech!

-- 
Karl Swartz		|INet	kls at ditka.chicago.com
1-408/223-1308		|UUCP	{uunet,decwrl}!daver!ditka!kls
			|Snail	1738 Deer Creek Ct., San Jose CA 95148
"It's psychosomatic.  You need a lobotomy.  I'll get a saw." (Calvin)



More information about the Comp.lang.c mailing list