Article View: net.lang.c
Article #9401Re: Expression sequencing query
From: Tom Kelly
Date: Fri, 26 Sep 1986 12:05
Date: Fri, 26 Sep 1986 12:05
48 lines
2722 bytes
2722 bytes
In article <3926@utah-cs.UUCP> donn@utah-cs.UUCP (Donn Seeley) discusses
the example:
a = ((b=1),b) + ((b=2),b) + ((b=3),b)
showing the partial order induced by the expression evaluation rules
of C language (as defined in K & R) and how various orders of
evaluation of the side effects are compatible with that partial
order. His conclusion is that the "correct" answer is an integer
between 3 and 9 inclusive. Various other people have posted the
answers obtained by various compilers.
The ANSI C committee (X3J11) has considered this question at some
length, mostly in the context of being able to write safe macros
that involve side-effects (prototypical example: getchar() + getchar().
Can the evaluation of the ?: operators in the "usual" implementation
of getchar be interleaved?).
The current draft (86-098: 9 July 1986) specifies in section 3.3 Expressions
(p. 31):
Except as indicated by the syntax, or otherwise specified
later (for the function-call operator (), the unary plus
operator, &&, ||, ?: and comma operators), the order
of evaluation of an expression is unspecified. The
implementation may evaluate subexpressions in any order,
even if the subexpressions produce side effects. The
order in which side effects take place is unspecified,
except that the evaluation of the operands of an
operator that involves a sequence point shall not be
interleaved with other evaluations.
Section 3.3.17 Comma Operator (p. 46)
The left operand of a comma operator is evaluated as a
void expression; there is a sequence point after its
evaluation. Then the right operand is evaluated; the
result has its type and value.
This has the result (along with the definition of sequence point) of
specifying that the value of "a" will be 6, since once one of the
comma expressions is started, it must be fully evaluated before any
of the others are. Of course, the resulting value of "b" is still
either 1, 2 or 3.
Tom Kelly (416) 922-1937
Human Computing Resources Corp.
{utzoo, ihnp4, decvax}!hcr!tom
Technical Details
Message-ID:
<1404@hcrvx1.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!hcrvx1!tomReferences:
<760@oakhill.UUCP> <111@titan.UUCP> <353@cullvax.UUCP> <111@unido.UUCP> <8200006@ztivax.UUCP> <3926@utah-cs.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site hcrvx1.UUCP Path: arc1-ca.pugleaf.net!utzoo!hcrvx1!tom From: tom@hcrvx1.UUCP (Tom Kelly) Newsgroups: net.lang.c Subject: Re: Expression sequencing query Message-ID: <1404@hcrvx1.UUCP> Date: Fri, 26-Sep-86 08:05:43 EDT Article-I.D.: hcrvx1.1404 Posted: Fri Sep 26 08:05:43 1986 Date-Received: Sat, 27-Sep-86 00:22:45 EDT References: <760@oakhill.UUCP> <111@titan.UUCP> <353@cullvax.UUCP> <111@unido.UUCP> <8200006@ztivax.UUCP> <3926@utah-cs.UUCP> Reply-To: tom@hcrvx1.UUCP (Tom Kelly) Organization: Human Computing Resources, Toronto Lines: 48